简体   繁体   中英

Android image share from internal storage not working

I have a functionality to share a particular image to Instagram, the image is downloaded from HTTP and stored in internal storage. When I try to share the image, it fails doing that.

// Store bitmap to image
String path = context.getFilesDir() + "/MyApp";
File imageFolder = new File(path);

if (!imageFolder.exists()) {
  imageFolder.mkdirs();
}

File imageFile = new File(path, UUID.randomUUID().toString() + ".jpg");

if (!imageFile.exists()) {
  imageFile.createNewFile();
}

FileOutputStream out = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();

// Share image
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageFile.toURI().toString()));
activity.startActivity(Intent.createChooser(share, "Share to"));

It doesn't throw any exception, but when instagram opens it says failed loading image and then gets closed.

In order to use file sharing you need to use a File Provider:

To securely offer a file from your app to another app, you need to configure your app to offer a secure handle to the file, in the form of a content URI. The Android FileProvider component generates content URIs for files, based on specifications you provide in XML

Please check the official documentation for an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM