Java 通过 ClipData.Item.getUri 暴露在应用程序之外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48117511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
exposed beyond app through ClipData.Item.getUri
提问by JonathanNet
I am trying to fix a problem after the new feature added in Android file system but I get this error:
我正在尝试在 Android 文件系统中添加新功能后解决问题,但出现此错误:
android.os.FileUriExposedException: file:///storage/emulated/0/MyApp/Camera_20180105_172234.jpg exposed beyond app through ClipData.Item.getUri()
So I hope some one can help me fix this :)
所以我希望有人能帮我解决这个问题:)
Thanks
谢谢
private Uri getTempUri() {
// Create an image file name
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory()
+ "/MyApp/", "Camera_" + dt + ".jpg");
AppLog.Log(
TAG,
"New Camera Image Path:- "
+ Environment.getExternalStorageDirectory()
+ "/MyApp/" + "Camera_" + dt + ".jpg");
File file = new File(Environment.getExternalStorageDirectory() + "/MyApp");
if (!file.exists()) {
file.mkdir();
}
imagePath = Environment.getExternalStorageDirectory() + "/MyApp/"
+ "Camera_" + dt + ".jpg";
imageUri = Uri.fromFile(imageFile);
return imageUri;
}
回答by eranda.del
Add following code block before start camera or file browsing
在开始相机或文件浏览之前添加以下代码块
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Please refer the referral link strict modeand its explained all the usage and technical details.
请参阅推荐链接严格模式及其解释所有用法和技术细节。
回答by Brendon
For sdk 24 and up, if you need to get the Uri of a file outside your app storage you have this error.
@eranda.del solutions let you change the policy to allow this and it works fine.
However if you want to follow the google guideline without having to change the API policy of your app you have to use a FileProvider.
对于 sdk 24 及更高版本,如果您需要获取应用程序存储之外的文件的 Uri,则会出现此错误。
@eranda.del 解决方案可让您更改策略以允许这样做,并且它运行良好。
但是,如果您想遵循 google 指南而不必更改应用程序的 API 策略,则必须使用 FileProvider。
At first to get the URI of a file you need to use FileProvider.getUriForFile() method:
首先要获取文件的 URI,您需要使用 FileProvider.getUriForFile() 方法:
Uri imageUri = FileProvider.getUriForFile(
MainActivity.this,
"com.example.homefolder.example.provider", //(use your app signature + ".provider" )
imageFile);
Then you need to configure your provider in your android manifest :
然后你需要在你的 android manifest 中配置你的提供者:
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.homefolder.example.provider"
android:exported="false"
android:grantUriPermissions="true">
<!-- ressource file to create -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
</application>
(In "authorities" use the same value than the second argument of the getUriForFile() method (app signature + ".provider"))
(在“authorities”中使用与 getUriForFile() 方法的第二个参数相同的值(应用程序签名 +“.provider”))
And finally you need to create the ressources file: "file_paths". This file need to be created under the res/xml directory (you probably need to create this directory too) :
最后,您需要创建资源文件:“file_paths”。这个文件需要在 res/xml 目录下创建(你可能也需要创建这个目录):
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
回答by Hemraj Patel
No required providerconfiguration into AndroidManifest only Camera and Storage permission should be permitted. Use the following code to start the camera:
AndroidManifest 中不需要提供程序配置,只有相机和存储权限应该被允许。使用以下代码启动相机:
final int REQUEST_ACTION_CAMERA = 9;
void openCamra() {
Intent cameraImgIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraImgIntent.putExtra(MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID +".provider",
new File("your_file_name_with_dir")));
startActivityForResult(cameraImgIntent, REQUEST_ACTION_CAMERA);
}
After capturing the image, you can find your captured image in:
捕获图像后,您可以在以下位置找到捕获的图像:
"your_file_name_with_dir"