繁体   English   中英

Android不会读取/写入文件

[英]Android will not read/write file

我正在尝试读取文件,

/storage/emulated/0/proj/Assets/Images/Screens/Title.png

我可以清楚地看到该文件存在,可以导航到该文件,可以打开该文件,我已验证代码已加载该文件,但是在运行我收到的项目时,

03-08 15:34:44.415 22192 22209 E AndroidRuntime: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: /storage/emulated/0/proj/Assets/Images/Screens/Title.png
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:88)
03-08 15:34:44.415 22192 22209 E AndroidRuntime: Caused by: java.io.FileNotFoundException: /storage/emulated/0/proj/Assets/Images/Screens/Title.png
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at android.content.res.AssetManager.openAsset(Native Method)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at android.content.res.AssetManager.open(AssetManager.java:313)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at android.content.res.AssetManager.open(AssetManager.java:287)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
03-08 15:34:44.415 22192 22209 E AndroidRuntime:    ... 13 more

权限如下,

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

这是代码(从多个类合并)

public static File getBaseDirectory() { 
    return Gdx.files.external("proj").file();
}

private static File getAssetsDirectory() {
    File dir = new File(FileUtils.getBaseDirectory(), "Assets");
    dir.mkdirs();
    return dir;
}

private static File getImagesDirectory() {
    File dir = new File(getAssetsDirectory(), "Images");
    dir.mkdirs();
    return dir;
}

private static File getScreensDirectory() {
    File dir = new File(getImagesDirectory(), "Screens");
    dir.mkdirs();
    return dir;
}

public static Texture getScreen(String name) {
    File f = new File(getScreensDirectory(), name);
    return new Texture(f.getAbsolutePath()); //<--where it is failing
}

细节,

Phone: Nexus 5
Android Version: 6.0
compileSdkVersion: 21
targetSdkVersion: 21

我自己的日志文件写入代码,

/storage/emulated/0/proj/log.txt

没有错误或问题。 我编写的记录器类从与导致问题的代码相同的方法集中获取位置。

File.exists()读取它确实存在。

为什么Android实际上确实存在时会引发java.io.FileNotFoundException?

您的代码( com.badlogic.gdx是您的代码,对吧?)正尝试使用AssetManager.open()打开文件。 该方法用于在APK中打开打包为资产的文件。 因此,例外情况是您的路径在APK的资产文件夹中不存在。

要使用直接路径打开文件,只需使用new FileInputStream(path)

libgdx Texture(String)不能按我在Android上的方式工作,此问题已通过更改来解决,

public static Texture getScreen(String name) {
    File f = new File(getScreensDirectory(), name);
    return new Texture(f.getAbsolutePath()); //<--where it is failing
}

至,

public static Texture getScreen(String name) {
    File f = new File(getScreensDirectory(), name);
    return new Texture(Gdx.files.absolute(f.getAbsolutePath())); //<--where it was failing
}

我缩小了PNG,就遇到了这个问题,然后当我尝试加载它们时,libgdx在我身上死了,并且找不到相同的文件。

libgdx库在某些缩小的PNG上存在问题,因此,如果您的PNG压缩为8bit,请尝试使用原始的PNG。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM