I want to share an image using the code:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);
I made a button that call the code above. The share intent open but I got if I click on "Share by MMS": "cannot add this picture to your message". If Facebook I got only a text area without my picture.
Intent sharingIntent = new Intent(Intent. ACTION_SEND); Uri imageUri = Uri. parse("http://stacktoheap.com/images/stackoverflow.png"); sharingIntent. setType("image/png"); sharingIntent.
Code Snippet: parse("android. resource://com.code2care.example.whatsappintegrationexample/drawable/image2"); Uri uri3 = Uri. parse("android. resource://com.code2care.example.whatsappintegrationexample/drawable/image3"); ArrayList<Uri> imageUriArray = new ArrayList<Uri>(); imageUriArray.
An adapted version of @eclass's answer which doesn't require use of an ImageView:
Use Picasso to load the url into a Bitmap
public void shareItem(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
startActivity(Intent.createChooser(i, "Share Image"));
}
@Override public void onBitmapFailed(Drawable errorDrawable) { }
@Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
}
Convert Bitmap into Uri
public Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
I use these codes from this tutorial
final ImageView imgview= (ImageView)findViewById(R.id.feedImage1);
Uri bmpUri = getLocalBitmapUri(imgview);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
then add this to your activity
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
then add your application manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With