WRITE_EXTERNAL_STORAGE不适用于棒棒糖,即使它已设置在清单中 [英] WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest

查看:18036
本文介绍了WRITE_EXTERNAL_STORAGE不适用于棒棒糖,即使它已设置在清单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像从应用程序本地数据文件夹保存到外部存储.我的清单包含以下内容(在清单的应用程序标记之前):

I'm trying to save images from the apps local data folders to external storage. My manifest contains the following (before the manifest's application tags):

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>

当我尝试以下操作

try {
        InputStream in = new FileInputStream(filePath);
        File outPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File outFile = new File(outPath, "mypicture.jpg");


        //try fails at this line
        OutputStream out = new FileOutputStream(outFile);

        byte[] buf = new byte[1024];
        int len;

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
} catch (IOException e) {
    e.printStackTrace();
}

我收到此错误:

java.io.FileNotFoundException: /storage/emulated/0/Pictures/mypicture.jpg: open failed: EACCES (Permission denied)

我还尝试了稍微不同的输出路径:

I've also tried a slightly different output path instead:

 String sdCardPath = Environment.getExternalStorageDirectory() + "/MyFolder";
 new File(sdCardPath).mkdirs();
 File outFile = new File(sdCardPath, "mypicture.jpg");

但这也给我一个错误:

java.io.FileNotFoundException: /storage/emulated/0/MyFolder/mypicture.jpg: open failed: ENOENT (No such file or directory)

该设备运行的是Android 4.4.2,因此无需在运行时请求权限(据我所知不能请求).

The device is running Android 4.4.2, so shouldn't need to request permissions at runtime (as far as I'm aware it can't request them).

为了允许将文件保存到外部存储,还有其他可能缺少的东西吗?

Is there something else that could be missing in order to allow saving a file to external storage?

推荐答案

问题的原因是通过gradle引入了一个外部库,该库具有自己的清单,请求<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18">

The cause of the issue was an external library pulled in via gradle which had its own manifest requesting <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18">

仅当不包括maxSdkVersion ="18"时,我自己的清单才有效,因此添加该参数的清单合并会导致此错误.我的解决方案是将自己的清单更改为:

My own manifest only worked if maxSdkVersion="18" was NOT included, and so the manifest merger adding that param caused this error. My solution was to change my own manifest to:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="23" tools:replace="android:maxSdkVersion" />

我假设maxSdkVersion ="18"表示任何运行SDK 19-22的设备都没有此权限(23+可以在运行时请求它).

I assume that the maxSdkVersion="18" meant that any devices running SDK 19-22 could not have this permission (with 23+ being able to request it at runtime).

这篇关于WRITE_EXTERNAL_STORAGE不适用于棒棒糖,即使它已设置在清单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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