9
File mediaDir = new File("media");
if (!mediaDir.exists()){
    mediaDir.createNewFile();
    mediaDir.mkdir();

}

File f = new File("/data/data/com.test.image/files/media/Voucher.jpg");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(b);
fos.close();

Try to write a file in phone internal memory but it throw an error: java.io.IOException: Read-only file system

shift66
  • 11,760
  • 13
  • 50
  • 83
chinna_82
  • 6,353
  • 17
  • 79
  • 134

5 Answers5

6

Guess /data/data is not external storage. You need to have root permission in order to write to the /data directory. Refer Data directory has no read/write permission in Android

Community
  • 1
  • 1
user936414
  • 7,574
  • 3
  • 30
  • 29
2

You are creating file in Android system's root directory for which no application is allowed to.

To create directory specific to your application, use getDir(String dirName, int mode) instead of new File("media"). By calling only, you can create media directory and use it.

No need to do so many stuffs what you are doing in your above code. Also no need to give any permission in manifest too.

AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • 3
    Though you have accepted answer, I don't think that is what you were looking for. /data/data is directory where all the application packages get installed. Your application package also will be maintained in this directory (/data/data/) only. So lets say your package name is **com.test.image**, in that case your media directory would be created whose path would be /data/data/com.test.image/app_media. So if you use getDir method of activity/context, you can directly access this /data/data folder and write file too. No need to write permissions in manifest. – AndroDev Feb 21 '12 at 08:35
1

You can try something like the following to make a folder in an Activity

this.getDir("folder_name", Context.MODE_PRIVATE);

source

shakram02
  • 10,812
  • 4
  • 22
  • 21
0

In my case just i changed in property file

   ## File upload custom properties
    file.upload.location=/upload-directory

to

   file.upload.location=upload-directory

Only removal of slash(/) helps me in simple spring boot file upload poc.

0

Use mediaDir.mkdirs(); instead of mediaDir.mkdir(); also you must require WRITE_EXTERNAL_STORAGE permission

KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33