3
\$\begingroup\$

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"/>
<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 runtime permissions but the LibGDX guys says that it works even on Android 6... Is there a fix With or Without runtime permissions?

\$\endgroup\$

closed as off-topic by Vaillancourt Feb 28 at 14:28

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.