代码startActivity(intent.createChooser(shareIntentWithJpgFileName))
在我的应用程序中已经运行了好几年了,但是大约一个月前,我注意到它不再将图像文件传递给用户在选择器对话框中选择的应用程序。我怀疑Android OS安全性遭到改组(再次!),但不确定如何进行。
如果用户在选择器对话框中选择了许多选择,则应用程序将打开,但不会显示意图的一部分.jpg
。如果用户选择Google云端硬盘,那么通过吐司,我会得到一个提示:
上传失败:请求中没有数据
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"));
此外,我将此作为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>
这也位于AndroidManifest.xml
中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
为什么它停止工作了,我又如何使其工作呢?
我很高兴维护/更新/修复/纠正/删除此问题,因此,如果由于任何原因它不令人满意,请给我指导。
我将Logcat
中的过滤器从仅显示选定的应用程序更改为无过滤器,并且能够查看所有应用程序的日志记录。可能弹出了大约50条线。很难说出哪些项目适用于我启动的意图,但是显然有些是我启动的意图(在选择器对话框中选择了Google云端硬盘)。我选择了一些有趣的日志记录条目:
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
我的下一步是检查“ EXTRA_STREAM”,我尚未完成,但我可能快要自己解决这个问题了。
答案 0 :(得分:5)
ACTION_SEND
将Intent.EXTRA_STREAM
用于the documentation而不是MediaStore.EXTRA_OUTPUT
的二进制内容。