Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a file on Android Internal Storage?

I want to save a file on internal storage into a specific folder. My code is:

File mediaDir = new File("media"); if (!mediaDir.exists()){    mediaDir.createNewFile();    mediaDir.mkdir();  } File f = new File(getLocalPath()); f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); fos.write(data); fos.close(); 

getLocalPath returns /data/data/myPackage/files/media/qmhUZU.jpg but when I want to create the media folder I'm getting the exception "java.io.IOException: Read-only file system". Any ideas how to write my files on internal phone storage in in folder media? Thanks.

like image 615
Buda Gavril Avatar asked Feb 16 '11 13:02

Buda Gavril


People also ask

How do I create an internal storage file?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken editext and button.

How do I use internal storage on Android?

All you have to do is open that app and select the "Show internal storage" option in its menu to browse through your phone's full internal storage. You can then open, move, rename, copy, delete, and share files as needed.


1 Answers

You should use ContextWrapper like this:

ContextWrapper cw = new ContextWrapper(context); File directory = cw.getDir("media", Context.MODE_PRIVATE); 

As always, refer to documentation, ContextWrapper has a lot to offer.

like image 157
Audrius Avatar answered Sep 22 '22 15:09

Audrius