使用LibGDX

时间:2016-02-13 13:51:11

标签: android file libgdx

我的create方法如下所示:

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

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

我还包括:

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

我得到的例外是:

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) 

我使用Nexus 5和Android 6 Marshmallow进行测试。 我搜索并发现Android 6使用运行时权限,但LibGDX开发人员说它甚至可以在Android 6上运行。是否有修复,无论有没有运行时权限

3 个答案:

答案 0 :(得分:2)

我通过检查用户是否已授予外部存储许可来解决问题,如果未通过使用授权请求权限请求:

            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);
            }

所有这些都是特定于Android的代码,因此位于AndroidLauncher类。

答案 1 :(得分:1)

我不相信Nexus 5有外部存储(SD卡)。

使用此测试来查找 -

boolean isExtAvailable = Gdx.files.isExternalStorageAvailable();

您可以找到更多信息here

答案 2 :(得分:0)

我有相同的情况(Android 6,Nexus 4 No SD Card,libGDX 1.9.3):
我用

检查了外部存储器
Gdx.files.isExternalStorageAvailable() // returns true

然后使用

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

然后致电

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

所以我检查了android端是这个代码真正可用的外部存储器:

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

这也是真的。所以它不是libGDX问题,而是Android问题。

然后我通过捕获异常来解决它,在这种情况下将文件写入本地存储。