当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?
示例代码:
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
Run Code Online (Sandbox Code Playgroud)
日志:
android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外
编辑:
在定位Android Nougat时,file://不再允许使用URI.我们应该使用content://URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?
合同已更改返回 Boolean而不是Bitmap从 androidx.activity 版本 1.2.0-alpha05 开始。如何使用Boolean内置AndroidResultContracts.TakePicture()合约返回的访问和显示用户刚刚拍摄的照片?
在 Fragment 中,我尝试从相机捕获图像并使用它。我想这样做,ActivityResultContracts.TakePicture()但是当我在捕获后尝试使用此图像时,我得到:
W/ImageView: Unable to open content: content://com.myniprojects.pixagram/my_images/default_image.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
我看过这个问题ActivityResultContracts.TakePicture()但我不知道我做错了什么。这是我所做的一切:
在此停靠到清单之后,我添加了:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.myniprojects.pixagram"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
在res/xml我创建的文件中file_paths:
<paths>
<files-path
name="my_images"
path="images/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
在片段中:
W/ImageView: Unable to open content: content://com.myniprojects.pixagram/my_images/default_image.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,这是完整的错误日志
2021-01-16 16:15:38.871 9433-9433/com.myniprojects.pixagram W/ImageView: Unable to open content: content://com.myniprojects.pixagram/my_images/default_image.jpg
java.io.FileNotFoundException: open failed: ENOENT (No such file …Run Code Online (Sandbox Code Playgroud)