Cant write nor read to/from external storage with 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 写入或读取外部存储
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01