【问题标题】:Permission denied while trying to open a file on external storage尝试在外部存储上打开文件时权限被拒绝
【发布时间】:2017-04-06 20:42:01
【问题描述】:

我收到了作为活动结果接收到的图像的内容 uri*

我只是想将该文件复制到另一个文件,但是当我尝试打开目标文件时,我不断收到权限被拒绝。有谁知道我做错了什么?

    Uri u = Uri.parse( data.getDataString());
    ContentResolver contentResolver = getContentResolver();

    try {
        InputStream is = contentResolver.openInputStream(u);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // media is not available to use. Abort.
            Toast.makeText(this, "Storage Unavailable.", Toast.LENGTH_LONG).show();
            return;
        }

        String dir = Environment.getExternalStorageDirectory().toString();
        File f = new File(dir, "test.jpg");
        f.createNewFile();
        // exception thrown
        // java.io.FileNotFoundException: /storage/emulated/0/test.jpg (Permission denied)
        FileOutputStream fose = new FileOutputStream(f); 

        byte[] b = new byte[1024];

        while (is.read(b) != -1) {
            fileOutputStream.write(b);
            fose.write(b);
        }
        fileOutputStream.close();
        is.close();
    } catch (IOException e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    ImageView iv = (ImageView) findViewById(R.id.theimage);
    iv.setImageURI(u);

在manifest中声明了外部写入权限,并已授予运行时权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

*这是使用的意图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
   startActivityForResult(Intent.createChooser(intent, "Select a File"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
   Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}

编辑:完整的堆栈跟踪:

6 15:23:27.068 6181-6181/test.example.x W/System.err: java.io.IOException: Permission denied
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at java.io.File.createNewFile(File.java:948)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at test.example.x.ui.MainActivity.onActivityResult(MainActivity.java:173)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6915)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
04-06 15:23:27.068 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.-wrap20(ActivityThread.java)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.os.Looper.loop(Looper.java:154)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
04-06 15:23:27.069 6181-6181/test.example.x W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

权限授予: http://i.imgur.com/AeWcmYG.png

【问题讨论】:

标签: android file permissions


【解决方案1】:

我遇到了同样的问题,可以通过以下任一步骤轻松删除它: 1. 如果在 Android N 和 O 版本上安装,请使用 -g 安装您的应用。 2.手动授予权限 设置->应用程序->“app_name”->权限->启用开关

对于第 1 步和第 2 步,定义使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE" 并使用权限 android:name="android.permission.READ_EXTERNAL_STORAGE" AndroidManifest.xml 中的属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 2019-10-06
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多