简体   繁体   中英

Android will not read/write file

I am trying to read the file,

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

I can clearly see the file exists, I can navigate to the file, I can open the file, I verified that the code loads that file, yet when running the project I receive,

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

Permissions are as follows,

<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"/>

Here is the code (consolidated from multiple classes)

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
}

Details,

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

My own code that writes the log file writes to,

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

without errors or issues. The logger class I wrote obtains the location from the same set of methods as the code causing issues.

File.exists() reads that it does exist.

Why is Android throwing a java.io.FileNotFoundException when it does in fact exist?

Your code (the com.badlogic.gdx stuff is yours, right?) is trying to open the file using AssetManager.open() . That method is for opening files packaged as assets in your APK . So the exception is saying that your path does not exist in your APK's asset folder.

To open a file with a direct path, just use new FileInputStream(path) .

libgdx Texture(String) does not work as I thought on Android, this issue was resolved by changing,

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

to,

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

I have had this problem with PNG's I have minified, and then when I try to load them, libgdx died on me with the same file not found error.

The libgdx library has problems with some minified PNG's, so if your PNG is compressed to 8bit, try with the original one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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