Image to content uri conversion issue in android - android

I have getting Image as String URL
But i have to pass this image as in content uri format
Something like this:- content://media/external/images/media/35001
How to do this??
I need the image URI instead of url then only i can share the image to social medias like twitter , facebook etc.But i am getting the image as string url.So i can't share the image.
below given is the code:-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// continue with your code
path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
image = Uri.parse(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_TEXT, "I posted this text " );
share.putExtra(Intent.EXTRA_STREAM, image);
share.setType("image/jpeg");
startActivity(Intent.createChooser(share, "Share"));

You can use following code to convert URL to URI:-
String your_url = "http://example.com.au/Images/8e6c8e7b-7b97-494d-ac8b-27064841dfd3.jpg";
URI uri = new URI(your_url.toString());

Related

How to share image of app

I have picture in drawable and I want to share that one image.
Following is my code,
btnic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.defaultpicture);
Intent ppp = new Intent("android.intent.action.SEND");
ppp.setType("image/png");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "defaultpicture", null);
Uri uri = Uri.parse(path);
ppp.putExtras(Intent.EXTRA_STREAM, uri); **i am getting error here**
MainActivity.this.startActivity(Intent.createChooser(ppp, "Send picture"));
Toast.makeText(getApplicationContext(), "Picture Copied", Toast.LENGTH_SHORT).show();
}
});
where am i doing wrong?
Intent.EXTRA_STREAM takes value as Local path of file on storage not on resource (Not a resource ID).
So you need to first save the image as file in storage then pass the url to sharing Intent .
First decode resource.
Bitmap b = BitmapFactory.decodeResource(res, id);
Save it to specific location on storage and use this file path as value in intent. you can save it as below or in some other way.
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "myFile", null);
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "yourimage.jpg");
Log.d("TAG", "onClick: "+photoFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));
for more about Sharing Content with Intents

Send Image as email attachement in android

I am getting an error that i cannot attach empty file in gmail.I am trying to build simple app in which when i click on the button it show choices by which i can send image,But the below code is not working.
Please help i am novice in android.
code:
if(view.getId()==R.id.SendImage)
{
Uri imageUri = Uri.parse("android:resource://com.example.jaspreet.intentstest.drawable/"+R.drawable.image);
intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);
intent.putExtra(Intent.EXTRA_TEXT,"Hey i have attached this image");
chooser=Intent.createChooser(intent,"Send Image");
startActivity(chooser);
}
Try using this:
shareIntent.setType("image/png");
By using this the intent know that it will send .png file.
On this link you can find a list of all media type/subtypes
Try this
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Try this snippet
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);
startActivity(Intent.createChooser(share, "Select"));
An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

Share Image in Twitter in Android using Intent

Hello I try to use the share dialog using Intent with the code below. I want to share an image and text at the same time. However I get the eror: Failed to insert image java.io.FileNotFoundException: No such file or directory
My code is as below, am I doing anything wrong. The code is in a fragment class.
Bitmap image = bmResized;
String pathOfBmp = Images.Media.insertImage(getActivity().getContentResolver(), image, "twitter_image.jpg", null);
Uri bmpUri = Uri.parse(pathOfBmp);
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setAction(Intent.ACTION_SEND);
tweetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "here is the tweet text");
tweetIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
tweetIntent.setType("image/jpeg");
startActivity(Intent.createChooser(tweetIntent, "Share this via"));
You can look Fabric library. For twitter process usually using Fabric library.
You can look Fabric document:
https://docs.fabric.io/android/twitter/compose-tweets.html
Also this is github link:
https://github.com/fabric/fabric
Try this:
Drawable mDrawable = mImageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setType("image/jpeg");
tweetIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(tweetIntent, "Share this via"));

photo share intent in android

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?
Try this
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Refer more links
http://android-developers.blogspot.in/2012/02/share-with-intents.html
Android Share Via Dialog
Image post into facebook wall from android sdcard
This is the solution I came up with:
This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.
shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"
public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
if ((intent != null)||(app.equals("others"))){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
if(!app.equals("others"))
share.setPackage(app);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
//startActivity(Intent.createChooser(share, "Share to"));
a.startActivity(share);
}
else{
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
intent.setData(Uri.parse("market://details?id="+app));
a.startActivity(intent);
}
}
You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.

Android: post image and text to Instagram

How can i post an image and a text to instagram?
no problem if i must open the instagram app, or if i do via api or other.
the problem is that i can't find how to: they have the iphone hook only, and the api is incomprensible.
someone have did that, or have the know?
thanks.
Try this it is worked for me. I will use it for share my product details. i will get details of product like name,image,description from the server and display in app and then share it to instagram.
Get image url from server.
URL url = ConvertToUrl(imgURL);
Bitmap imagebitmap = null;
try {
imagebitmap = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Post image and text on instagram.
// post on instagram
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,
getImageUri(HomeActivity.this, result));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, sharename);
shareIntent.putExtra(
Intent.EXTRA_TEXT,
"Check this out, what do you think?"
+ System.getProperty("line.separator")
+ sharedescription);
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
Convert Uri String to URL
private URL ConvertToUrl(String urlStr) {
try {
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(),
url.getHost(), url.getPort(), url.getPath(),
url.getQuery(), url.getRef());
url = uri.toURL();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Convert Bitmap to Uri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(
inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
I doubt the Instagram developers will ever release iPhone like hooks for android as Intents already serve this purpose.
If you want to share an image from your app use an intent like this:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpeg");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Path/To/Image.jpg"));
startActivity(Intent.createChooser(i, "Share Image"));
Note that this will show a dialoge allowing the user to pick which photo sharing app to launch.
This works for me.... but it assumes the package name of com.instagram.android, which may change without notice I suppose.
private void shareToInstagram(Uri imageUri){
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, imageUri);
i.setPackage("com.instagram.android");
activity.startActivity(i);
}
You could always check to see if the com.instagram.android package is installed using getPackageManager() ... and if not, then don't set the package name in the intent and let the app chooser help the user pick the app to share the image with.

Categories

Resources