# Cant write/read to/from external storage with LibGDX [closed]

My create method looks like this:

@Override
public void create () {
batch = new SpriteBatch();

FileHandle file = Gdx.files.external("file.txt");
file.writeString("My god, it's full of stars", false);
}


I allso included:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


The Exception i get is this:

02-13 14:45:51.858 12439-12466/com.snowdevs.tweetiebirds E/AndroidRuntime: FATAL EXCEPTION: GLThread 1120
Process: com.snowdevs.tweetiebirds, PID: 12439
com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: file.txt (External)
at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: file.txt (External)
at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/file.txt: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)


I use a Nexus 5 with Android 6 Marshmallow for testing. I searched and found that Android 6 uses runtime permissions but the LibGDX guys says that it works even on Android 6... Is there a fix With or Without runtime permissions?

• I'm voting to close this question because it is a cross-post from stackoverflow. Cross-posting is not allowed on StackExchange sites. – Vaillancourt Feb 28 at 14:28
• But thanks for coming back and marking it as accepted :) – Vaillancourt Feb 28 at 14:29

If you're writing to the local area (Where the app is stored) then don't use the external file type. The correct type to use is local as the local type will store it in the same folder as the app. The problem with external is that external reads from the root directory onward. So on Windows the external path would need to specify the drive and other such specialized locations on the hard drive such as C:/Windows/system32.sys or F:/Videos/Other/NotAdultVideo.mp4 whereas using the local format allows you to store it in the local area near to your app run-time and such.

This question is now answered in here, and it goes like this:

I check if the user has granted permission of external storage and request permission if not granted by using:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_REQUEST_CODE);
}


All of this is android-specific code and thus resides in the AndroidLauncher class.