Android使用LibGDX访问文件

huangapple 未分类评论74阅读模式
英文:

Android with LibGDX get access to a file

问题

我在使用libGDX时遇到了读取Android文件的问题。

我使用的工具如下:

  • Lib GDX 1.9.11
  • Android Studio 4.2
  • Java 1.8.0_261
  • Android Gradle 4.0.1
  • Gradle 6.6
  • Android SDK 30-3
  • Android 8.1 x86_64

首先,在XML文件中实现了权限:

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

其次,我请求用户允许访问存储:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    while (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_REQUEST_CODE);
    while (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        this.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_REQUEST_CODE);
}

(一开始我使用了 while 循环来重新询问用户,如果他选择了拒绝,但这并没有起作用,我没有勾选“不再询问”,但对话框没有再次弹出)

它可以正常工作:https://i.stack.imgur.com/v57uG.png

为了创建文件夹和读/写文件,我使用了FileHandle:

public static boolean createFolder(String path) {
    final FileHandle file = Gdx.files.local(path);
    if (!file.exists()) {
        file.mkdirs();
        return true;
    }
    return false;
}
...
final FileHandle playerFile = Gdx.files.local("data/" + fileName + ".dat");
playerFile.writeString(content, false);
...
...
final FileHandle playerFile = Gdx.files.local("data/" + fileName + ".dat");
if (!playerFile.exists())
    return null;
final Scanner reader = new Scanner(playerFile.file());
...

问题是读取/写入的访问被拒绝:

type=1400 audit(0.0:11): avc: denied { write } for comm=656374696F6E207072696D6172795D name="perfd" dev="dm-0" ino=7181 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:shell_data_file:s0 tclass=dir permissive=0
type=1400 audit(0.0:10): avc: denied { read } for name="u:object_r:serialno_prop:s0" dev="tmpfs" ino=4438 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serialno_prop:s0 tclass=file permissive=0

我阅读了这个主题:https://stackoverflow.com/questions/35380746/cant-write-nor-read-to-from-external-storage-with-libgdx/44028465#44028465

但是我使用的是 local 文件,所以我不知道为什么会出现这个问题...

提前谢谢!

英文:

i have an issue to read android files with libGDX

I'm using :

  • Lib GDX 1.9.11
  • Android Studio 4.2
  • Java 1.8.0_261
  • Andoid Gradle 4.0.1
  • Gradle 6.6
  • Android SDK 30-3
  • Android 8.1 x86_64

First, i've implemented permissions in the XML file :

&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;

Second, i ask user to allow access to the storage :

if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
	while (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
		this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_REQUEST_CODE);
	while (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
		this.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_REQUEST_CODE);
}

(At the beginning i've used while to re-ask user if he picked deny, but it doesn't work, i didn't check don't ask again but dialog box doesn't pop again)

It works correctly : https://i.stack.imgur.com/v57uG.png

To create folders and read/write files i'm using FileHandle :

public static boolean createFolder(String path) {
    final FileHandle file = Gdx.files.local(path);
    if (!file.exists()) {
        file.mkdirs();
        return true;
    }
    return false;
}
...
final FileHandle playerFile = Gdx.files.local(&quot;data/&quot; + fileName + &quot;.dat&quot;);
playerFile.writeString(content, false);
...
...
final FileHandle playerFile = Gdx.files.local(&quot;data/&quot; + fileName + &quot;.dat&quot;);
if (!playerFile.exists())
    return null;
final Scanner reader = new Scanner(playerFile.file());
...

Problem is access to read / write is denied :

type=1400 audit(0.0:11): avc: denied { write } for comm=656374696F6E207072696D6172795D name=&quot;perfd&quot; dev=&quot;dm-0&quot; ino=7181 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:shell_data_file:s0 tclass=dir permissive=0
type=1400 audit(0.0:10): avc: denied { read } for name=&quot;u:object_r:serialno_prop:s0&quot; dev=&quot;tmpfs&quot; ino=4438 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serialno_prop:s0 tclass=file permissive=0

I've read this topic : https://stackoverflow.com/questions/35380746/cant-write-nor-read-to-from-external-storage-with-libgdx/44028465#44028465

But i'm using local files so i don't know where it comes...

Thank you in advance Android使用LibGDX访问文件

huangapple
  • 本文由 发表于 2020年8月15日 01:39:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/63417614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定