Data directory has no read/write permission in Android

前端 未结 1 885
你的背包
你的背包 2020-11-30 01:03

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

System.out.println(\"DAta can write??--->\"+Environment.getDataDirectory         


        
相关标签:
1条回答
  • 2020-11-30 01:18

    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.

    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.

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

    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" />
    

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

    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));
    
    0 讨论(0)
提交回复
热议问题