使用Android创建文件时,即使获得了权限,也会出现“权限被拒绝"错误 [英] Getting a Permission denied error when creating a file with Android, even when permission is given

查看:1706
本文介绍了使用Android创建文件时,即使获得了权限,也会出现“权限被拒绝"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的清单中有这个

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

这是我尝试创建和写入文件的位置(该文件位于名为EnterUserInfo.java的文件中:

This is where I am trying to create and write to a file (it is in a file called EnterUserInfo.java:

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};


/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        System.out.println("INSIDEEEEEE");
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    } else {
        System.out.println("HEREEEEEEEEE");
    }
}

private void writeToFile(String data, Context context) {

    verifyStoragePermissions(this);
    String FILENAME = "new_clients.txt";
    String string = "hello world!";

    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        FileOutputStream fos = context.openFileOutput("new_clients.txt", Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

当我尝试创建文件时,出现的内容是:

When I try to create a file, this is what appears:

I/System.out: HEREEEEEEEEE
W/ContextImpl: Failed to ensure /data/user/0/c.b.project/files: mkdir failed: EACCES (Permission denied)
W/FileUtils: Failed to chmod(/data/user/0/cs.b07.cscb07courseproject/files): android.system.ErrnoException: chmod failed: EACCES (Permission denied)
W/System.err: java.io.FileNotFoundException: /data/user/0/c.b.project/files/new_clients.txt (Permission denied)
W/System.err:     at java.io.FileOutputStream.open(Native Method)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err:     at android.app.ContextImpl.openFileOutput(ContextImpl.java:506)
W/System.err:     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:192)
W/System.err:     at EnterUserInfo.writeToFile(EnterUserInfo.java:69)

如您所见,它将显示here,表示已授予权限,但紧随其后会出现Permission Denied错误.知道如何解决这个问题吗?

As you can see, it prints here meaning the permission is granted, but right after it gives a Permission Denied error. Any idea how to solve this?

附带说一句,当它说它试图保存到/data/user/0/cs.b07.cscb07courseproject/files时,是在项目内还是保存在我的计算机上?因为当我去终端并执行cd /data/cd /data时都找不到.

On a side note, when it says that it tries to save to /data/user/0/cs.b07.cscb07courseproject/files, is that within the project or is that saved on my computer? Because when I go to my terminal and do cd /data/ or cd /data neither is found.

writeToFile()是在与上面发布的类和文件相同的名称中调用的,这是代码(当用户在UI中单击注册"按钮时,将调用以下功能:

writeToFile() is called in the same class and file posted above, and this is the code (the function below is called when a user hits the "register" button in the UI:

public void createNewUser(View view) {
    // a data string is created here:
    // String data = "asd";
    writeToFile(data, this);
}

请注意,我确实在运行时使用verifyStoragePermissions()方法请求权限.除非请求权限的方式有问题(我不认为这是因为确实出现了提示询问用户许可的提示),否则我认为问题出在其他方面.

Edit 2: Please note that I did ask for permission at runtime in my verifyStoragePermissions() method. Unless something is wrong with that way of asking for permission (which I don't think it is because a prompt does appear which asks the user for permission), then I think the issue is with something else.

推荐答案

您不需要任何权限即可调用openFileOutput().这会将文件写入应用程序专用的专用于专用应用程序的数据区域.

You do not need any permissions to call openFileOutput(). This writes a file to the private application-specific data area, which is owned by your application.

根据以下错误判断:

W/ContextImpl: Failed to ensure /data/user/0/c.b.project/files: mkdir failed: EACCES (Permission denied)
W/FileUtils: Failed to chmod(/data/user/0/cs.b07.cscb07courseproject/files): android.system.ErrnoException: chmod failed: EACCES (Permission denied)

似乎有人更改了应用程序的私有数据目录/data/user/0/c.b.project/上的文件所有权(或访问权限).该目录应归您的应用程序用户ID所拥有,因此您的应用程序应具有写入该目录的必要权限.

It looks like someone has changed the file ownership (or access rights) on your application's private data directory /data/user/0/c.b.project/. This directory should be owned by your application's user ID and therefore your application should have the necessary rights to write to it.

卸载您的应用程序(应该删除该目录),然后重新安装您的应用程序(应该使用正确的权限重新创建目录).

Uninstall your app (which should delete that directory) and then reinstall your app (which should recreate the directory with the correct permissions).

这篇关于使用Android创建文件时,即使获得了权限,也会出现“权限被拒绝"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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