1

I am trying to create a file in the internal storage when I say internal storage it is not to keep safe data, it will be to save some images or public files I was trying everything but it is giving me error and I don't know why D:

CODE:

File file = new File(Environment.getDataDirectory() + File.separator
        + "test1111111.txt");
Log.e("ERROR", Environment.getDataDirectory() + File.separator
        + "testfile.txt");
try {
    file.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}
byte[] data1 = {
        1, 1, 0, 0
};
// write the bytes in file
if (file.exists())
{
    OutputStream fo = null;
    try {
        fo = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fo.write(data1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("file created: " + file);

}

ERROR:

04-24 03:41:22.704    7797-7797/com.test.test W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-24 03:41:22.744    7797-7797/com.test.test E/ERROR﹕ /data/testfile.txt
04-24 03:41:22.744    7797-7797/com.test.test W/System.err﹕ java.io.IOException: open failed: EACCES (Permission denied)
04-24 03:41:22.744    7797-7797/com.test.test W/System.err﹕ at java.io.File.createNewFile(File.java:940)

MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.test" >

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

In the emulator works fine but when I try with my phone it is giving me error. EMULATOR: enter image description here

DEVICE: It is giving me error.

EDITED

Cœur
  • 37,241
  • 25
  • 195
  • 267
kobbycoder
  • 682
  • 2
  • 10
  • 33

2 Answers2

3

Data directory is a system's directory, you can never have the permission to write in it.

Try for example :

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "testfile.txt");

See this : Data directory has no read/write permission in Android

Community
  • 1
  • 1
Souhaib Guitouni
  • 860
  • 1
  • 7
  • 24
1

You wont have the permission to write file under /data folder. Perhaps you can try SD card or Android Internal Storage instead.

landry
  • 561
  • 1
  • 7
  • 19
  • can you guide me? i have edited my post, and posted a picture about something that i want. it worked with the emulator but not with the phone – kobbycoder Apr 24 '15 at 08:15
  • Some phones have SD card inside already. SD card here means Environment.getExternalStorageDirectory() here. android internal storage means /data/data/yourpackagename/. you can get internal storage with context.getFilesDir(). Please refer detail information from http://stackoverflow.com/questions/1998400/data-directory-has-no-read-write-permission-in-android – landry Apr 24 '15 at 08:16