Bashar
Bashar

Reputation: 51

Cant write nor read to/from external storage with LibGDX

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 also included:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_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.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:353)
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:339)
    at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22)
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254)
    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.badlogic.gdx.files.FileHandle.writer(FileHandle.java:330)
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:350)
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:339) 
    at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22) 
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254) 
    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.badlogic.gdx.files.FileHandle.writer(FileHandle.java:322)
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:350) 
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:339) 
    at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22) 
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254) 
    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.badlogic.gdx.files.FileHandle.writer(FileHandle.java:322) 
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:350) 
    at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:339) 
    at com.snowdevs.tweetiebirds.TweetieBirdsGame.create(TweetieBirdsGame.java:22) 
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254) 
    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 Run Time Permissions, but the LibGDX developers say that it works even on Android 6. Is there a fix, either with or without Run Time Permissions?

Upvotes: 3

Views: 1879

Answers (3)

Bashar
Bashar

Reputation: 51

I solved the problem by checking if the user has granted permission of external storage and request permision 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.

Upvotes: 2

gaRos
gaRos

Reputation: 2783

I had the same situation(Android 6, Nexus 4 No SD Card, libGDX 1.9.3):
I checked the external storage with

Gdx.files.isExternalStorageAvailable() // returns true

then get the fileHandle with

FileHandle file = Gdx.files.external("file.txt"); // no exception

then call

file.writeString("text",false); // throws GdxRuntimeException

So I checked on android side is the external storage really available with this code:

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); // returns true

This also returned true. So it's not a libGDX issue, rather than an Android issue.

then I solved it with catching the exception and in that case writing the file to local storage.

Upvotes: 1

Rupert
Rupert

Reputation: 11

I don't believe Nexus 5 has external storage (SD card).

Use this to test to find out -

boolean isExtAvailable = Gdx.files.isExternalStorageAvailable();

You can find more information here.

Upvotes: 1

Related Questions