我試圖讀取該文件,Android將無法讀/寫文件
/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?
您正在測試哪種Android版本?另外,你的應用程序的'targetSdkVersion'是什麼? – CommonsWare
@CommonsWare我添加了compileSdkVersion,但找不到targetSdkVersion,我會在哪裏找到這些信息?對不起,我在使用libgdx和Android開發方面都很新。 – 1ncline
需要一些代碼。你在路徑之前指定了'file://? – user3479671