Reputation: 53
I want to share an image using ACTION_SEND. So basically when the users taps on an image and selects "share image" it should send the image selected, So when testing, it takes me to which app I want to use to share with i.e whatsapp, Facebook, email etc. And then when selecting either one, it then says "sharing failed, please try again." I can't seem to figure out why it doesn't work. However I have the same code to display image file full screen with ACTION_VIEW and that seems to work great but not with sharing.
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
Upvotes: 0
Views: 1578
Reputation: 53
This worked for me
First stored my image in external storage
private void SaveImage(Bitmap finalBitmap) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "image.jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
}
## . ##
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ahmed);
SaveImage(img);
}
then load my image from external storage then shared it .
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/saved_images/image.jpg"));
startActivity(Intent.createChooser(share, "Share image using"));
}
got the path from emulator
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: Scanned /storage/emulated/0/saved_images/image.jpg:
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: -> uri=content://media/external/images/media/106290
Upvotes: 1
Reputation: 3238
step 1: First add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
step 2: edit
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath);
Upvotes: 0
Reputation: 99
set type image/jpg instead of image/*
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
Upvotes: 1