在AndroidManifest.xml中声明FileProvider
在res/xml下创建一个名为file_paths.xml的文件(如果该目录下不存在,请手动创建)。示例代码如下。
使用FileProvider.getUriForFile()方法获取文件的URI。 File file = new File(Environment.getExternalStorageDirectory(), "example.jpg"); Uri photoURI = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", file);
将URI添加到Intent中 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(photoURI, "image/*"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent);
确保您的应用有读取外部存储器的权限。如果本地文件无法访问,请请求适当的权限。
这些步骤将确保您的Android应用程序能够正确地使用文件提供程序查看图像。
下一篇:Android文件未删除