Android - 没有WRITE_EXTERNAL_STORAGE权限将文件转换为内容URI?

时间:2014-12-23 18:23:19

标签: android android-contentresolver

我目前使用此方法有效地将图像文件转换为内容uri ...

protected static Uri convertFileToContentUri(Context context, File file) throws Exception {

    ContentResolver cr = context.getContentResolver();
    String imagePath = file.getAbsolutePath();
    String imageName = null;
    String imageDescription = null;
    String uriString = MediaStore.Images.Media.insertImage(cr, imagePath, imageName, imageDescription);
    return Uri.parse(uriString); }

...但问题是它需要android.permission.WRITE_EXTERNAL_STORAGE权限。

有没有办法在不需要许可的情况下执行相同的转换?

1 个答案:

答案 0 :(得分:3)

那不“将图像文件转换为内容uri”。这会在MediaStore ContentProvider中插入一个条目,恰好会给你一个Uri。但是,现在任何东西都可以获得该内容。这类似于在Pastebin上发布您的个人联系信息,因为您觉得您需要一个URL,然后想知道为什么突然之间您会收到一堆非常奇怪的电话。 URL是发布个人联系信息的副作用; Uri是发布图像的副作用。

如果您想通过ContentProvider投放文件,请在您的应用中添加ContentProvider。根据文件所在的位置,您可以使用Android支持包(support-v4support-v13)提供的the FileProvider。或者,通过覆盖ContentProvider滚动您自己的广告openFile()

相关问题