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

Data directory has no read/write permission in Android(数据目录在Android中没有读/写权限)
本文介绍了数据目录在Android中没有读/写权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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());
        }
    }

推荐答案

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

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.

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

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.openFileOutput() 方法.

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

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

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:

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

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

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

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

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.

所以在您的代码中考虑如下内容:

So in your code consider something like:

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

这篇关于数据目录在Android中没有读/写权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)