通过ClipData.Item.getUri在应用程序之外公开 [英] exposed beyond app through ClipData.Item.getUri

查看:74
本文介绍了通过ClipData.Item.getUri在应用程序之外公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android文件系统中添加了新功能后,我正在尝试解决问题,但出现此错误:

I am trying to fix a problem after the new feature added in Android file system but I get this error:

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 :)

谢谢

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;
}

推荐答案

对于sdk 24及更高版本,如果您需要在应用程序存储之外获取文件的Uri,则会出现此错误.
@ eranda.del解决方案使您可以更改策略以允许此操作,并且效果很好.

但是,如果您想遵循Google指南而不必更改应用程序的API政策,则必须使用FileProvider.

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.

首先要获取文件的URI,需要使用FileProvider.getUriForFile()方法:

At first to get the URI of a file you need to use FileProvider.getUriForFile() method:

Uri imageUri = FileProvider.getUriForFile(
            MainActivity.this,
            "com.example.homefolder.example.provider", //(use your app signature + ".provider" )
            imageFile);

然后,您需要在android清单中配置您的提供程序:

Then you need to configure your provider in your 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>

(在当局"中,使用与getUriForFile()方法的第二个参数(应用程序签名+".provider")相同的值)

(In "authorities" use the same value than the second argument of the getUriForFile() method (app signature + ".provider"))

最后,您需要创建资源文件:"file_paths". 此文件需要在res/xml目录下创建(您可能也需要创建此目录):

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) :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

这篇关于通过ClipData.Item.getUri在应用程序之外公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆