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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With