数据目录在Android中没有读/写权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:08:30  来源:igfitidea点击:

Data directory has no read/write permission in Android

androidandroid-emulatorandroid-manifest

提问by Vinayak Bevinakatti

I m using Android 1.5 my data directory doesn't have the read/write permissions

我使用的是 Android 1.5 我的数据目录没有读/写权限

System.out.println("DAta can write??--->"+Environment.getDataDirectory().canWrite());
System.out.println("DAta can read??--->"+Environment.getDataDirectory().canRead());

So please suggest me how to provide permissions for the data directory.

所以请建议我如何为数据目录提供权限。

What I'm trying to do is to create a file and add some content to it in the Data storage of the emulator like as below

我想要做的是创建一个文件并在模拟器的数据存储中添加一些内容,如下所示

private void writeToSDCard() {
        try {

            File lroot = Environment.getDataDirectory();

            if (lroot.canWrite()){
                File lfile = new File(lroot, "samplefile.txt");
                FileWriter lfilewriter = new FileWriter(lfile);
                BufferedWriter lout = new BufferedWriter(lfilewriter);
                lout.write("XXXXXXXXXXXXXXXXXX");
                lout.close();
            }
        } catch (IOException e) {
            Log.e(m_cTAG, "Could not write file " + e.getMessage());
        }
    }

回答by Dave Webb

You shouldn't be looking at the Data Directory. This is a system directory in the phone's storage - usually /data- and your application will never have permission to write to it.

您不应该查看数据目录。这是手机存储中的系统目录 - 通常/data- 您的应用程序永远没有写入权限。

The directory your application should write files to is returned by the Context.getFilesDir()method. It will be something like /data/data/com.yourdomain.YourApp/files.

您的应用程序应将文件写入的目录由Context.getFilesDir()方法返回。它会像/data/data/com.yourdomain.YourApp/files.

If you want to write to a file in the phone's storage use the Context.openFileOutput()method.

如果要写入手机存储中的文件,请使用Context.openFileOutput()方法

If you want the path to the SDCard then use Environment.getExternalStorageDirectory()method. To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest:

如果您想要 SDCard 的路径,请使用Environment.getExternalStorageDirectory()method。要写入 SDCard,您需要通过将以下内容添加到您的清单中来为您的应用程序授予适当的权限:

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

If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState()method.

如果您要写入 SDCard,您还需要使用该getExternalStorageState()方法检查其状态。

If you're storing small files to do with your application then these can go into the phone's storage and not the SD Card, so use the Context.openFileOutput()and Context.openFileInput()methods.

如果您要存储与应用程序有关的小文件,那么这些文件可以进入手机的存储空间而不是 SD 卡,因此请使用Context.openFileOutput()Context.openFileInput()方法。

So in your code consider something like:

所以在你的代码中考虑如下:

OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));