Another new breaking changes for Android 11, attaching image is no longer working with Intent
FileProvider XML
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
AndroidManifest XML
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
This code will save image and triggers the intent, I retested it and it does save a file and return Uri starting with content://
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
//Create bitmap screen capture
Bitmap bitmap = Bitmap.createBitmap(loadBitmapFromView(refView));
final String relativeLocation = Environment.DIRECTORY_PICTURES + "/" + refView.getContext().getString(R.string.app_name);
final ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, UUID.randomUUID().toString() + ".png");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png"); //Cannot be */*
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, true);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
final ContentResolver resolver = refView.getContext().getContentResolver();
try {
Uri uriResolve = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
if (uriResolve == null || uriResolve.getPath() == null) {
throw new IOException("Failed to create new MediaStore record.");
}
try (OutputStream stream = resolver.openOutputStream(uriResolve)){
if (stream == null) {
throw new IOException("Failed to get output stream.");
}
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
throw new IOException("Failed to save bitmap.");
}
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, false);
resolver.update(uriResolve, contentValues, null, null);
shareToInstant("blablabla", uriResolve, refView.getContext())
} catch (IOException e) {
//Don't leave an orphan entry in the MediaStore
resolver.delete(uriResolve, null, null);
e.printStackTrace();
FirebaseCrashlytics.getInstance().recordException(e);
}
}
private static void shareToInstant(String content, Uri uri, View view) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
sharingIntent.putExtra(Intent.EXTRA_TEXT, content.replace(view.getContext().getString(R.string.show_more), "").replace(view.getContext().getString(R.string.show_less), "") + "\nFor more detail download app here : https://play.google.com/store/apps/details?id=" + view.getContext().getPackageName());
try {
view.getContext().startActivity(Intent.createChooser(sharingIntent, "Share it Via"));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(view.getContext(), R.string.unknown_error, Toast.LENGTH_SHORT).show();
}
}
You can ignore the FileProvider since I am using it for older version which is not included in the sample code.
I read some post like looping with ResolveInfo
but I don't think that is the real solution, but rather a temporary hack. I wanted to know the culprit behind this and why, providing a source/documentation pointing to this changes is also a big help.
GMail's - Toast "Unable to attach file."
Twitter - Freezes (black screen)
Telegram - Working
Instagram - Not working
Skype - Not working
Viber - Only Image no text
Messenger - Only Image no text