问题:
The app is crashing when I'm trying to open a file. 当我尝试打开文件时,应用程序崩溃。 It works below Android Nougat, but on Android Nougat it crashes. 它在Android Nougat以下运行,但是在Android Nougat上崩溃。 It only crashes when I try to open a file from the SD card, not from the system partition. 仅当我尝试从SD卡而不是系统分区打开文件时,它才会崩溃。 Some permission problem? 一些权限问题?
Sample code: 样例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Log: 日志:
android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()在应用程序之外公开
Edit: 编辑:
When targeting Android Nougat, file://
URIs are not allowed anymore. 定位Android牛轧糖时,不再允许file://
URI。 We should use content://
URIs instead. 我们应该改用content://
URI。 However, my app needs to open files in root directories. 但是,我的应用程序需要打开根目录中的文件。 Any ideas? 有任何想法吗?
解决方案:
参考一: https://stackoom.com/question/2aHda/android-os-FileUriExposedException-file-storage-emulated-test-txt通过Intent-getData-在应用程序之外公开参考二: https://oldbug.net/q/2aHda/android-os-FileUriExposedException-file-storage-emulated-0-test-txt-exposed-beyond-app-through-Intent-getData
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4262381