The code startActivity(intent.createChooser(shareIntentWithJpgFileName))
had been working fine in my application for years, but I noticed about a month ago that it no longer passes the image file to the application that the user selects in the chooser dialog. I suspect Android OS security shuffling (again!), but not sure how to proceed.
If the user selects any number of choices in the chooser dialog, the application opens, but doesn't show the .jpg
that was part of the intent. If the user selects Google Drive, I get a hint of what's wrong through it's toast:
Upload unsuccessful: request contained no data
String fileName = data.getStringExtra(EXTRA_FILENAME);
Log.v(TAG, "Starting Share intent. fileName: " + fileName); //This shows fileName: /storage/emulated/0/dcim/DalesApp/IMG_20190103_142555_8445288886207383328.jpg
Intent shareIntentWithJpgFileName = new Intent(Intent.ACTION_SEND);
shareIntentWithJpgFileName.setType("image/jpeg");
File filePassedIn = new File(fileName);
Log.v(TAG, "exists: " + filePassedIn.exists() + " canRead: " + filePassedIn.canRead()); // Result is exists: true canRead: true. Thanks Mark!
shareIntentWithJpgFileName.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", filePassedIn));
shareIntentWithJpgFileName.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntentWithJpgFileName, "Share Image"));
Also, I have this as part of the AndroidManifest.xml
:
<provider
android:name="android.support.v4.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>
And this is also in the AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Why has this stopped working and how can I get it working again?
I would be pleased to maintain/update/fix/correct/delete this question, so please give me guidance if you find it unsatisfactory for any reason.
I changed the filter in Logcat
from Show only selected application to No Filters and was able to see logging from all applications. There were probably 50 or so lines that popped-up. It's hard to tell which items are applicable to the intent I started, but some are obviously from the intent I started (selected Google Drive in the chooser dialog). I picked a few more interesting logging entries:
I/ActivityManager: START u0 {act=android.intent.action.SEND typ=image/jpeg flg=0xb080001 cmp=com.google.android.apps.docs/.shareitem.UploadMenuActivity (has extras)} from uid 10196
E/SchedPolicy: open of /dev/cpuctl/bg_non_interactive/tasks failed: No such file or directory
I/ActivityManager: Start proc 12705:com.google.android.apps.docs/u0a92 for activity com.google.android.apps.docs/.shareitem.UploadMenuActivity
E/AsyncTask #3-DataSourceHelper: Uploading single file but neither EXTRA_STREAM nor EXTRA_TEXT is specified.
E/main-UploadMenuActivity: No files requested to be uploaded: android.intent.action.SEND
I/LaunchCheckinHandler: Displayed com.google.android.apps.docs/.shareitem.UploadMenuActivity,cp,ca,570
My next step is checking into "EXTRA_STREAM", which I haven't done yet, but I might be close to solving this on my own.
ACTION_SEND
uses Intent.EXTRA_STREAM
for binary content as per the documentation, not MediaStore.EXTRA_OUTPUT
.