无法使用 LibGDX 写入或读取外部存储

Cant write nor read to/from external storage with LibGDX(无法使用 LibGDX 写入或读取外部存储)

本文介绍了无法使用 LibGDX 写入或读取外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 create 方法如下所示:

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

我还包括:

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

我使用带有 Android 6 Marshmallow 的 Nexus 5 进行测试.我搜索并发现 Android 6 使用 Run Time Permissions,但是 LibGDX 开发人员说它甚至可以在 Android 6 上工作.有没有修复,无论是否有 运行时权限?

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?

推荐答案

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

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

所有这些都是 android 特定的代码,因此驻留在 AndroidLauncher 类中.

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

这篇关于无法使用 LibGDX 写入或读取外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法使用 LibGDX 写入或读取外部存储

基础教程推荐