当前位置: 动力学知识库 > 问答 > 编程问答 >

android - getting FileNotFoundException even thought the file is there

问题描述:

I am trying to access a file for xml parsing with this:

InputStream inputStream = context.getAssets().open("/mnt/sdcard/cat/bird/flowers.xml");

When it runs i get the following error:

06-13 23:13:22.620: W/System.err(3118): java.io.FileNotFoundException: /mnt/sdcard/cat/bird/flowers.xml

06-13 23:13:22.620: W/System.err(3118): at android.content.res.AssetManager.openAsset(Native Method)

06-13 23:13:22.620: W/System.err(3118): at android.content.res.AssetManager.open(AssetManager.java:314)

06-13 23:13:22.620: W/System.err(3118): at android.content.res.AssetManager.open(AssetManager.java:288)

I have checked the spelling of the file and i have checked the location several times. The file is there and correctly named. What is going on here?

Thanks

网友答案:

getAssets().open(...) is used to access files which you have packaged with the .apk file by placing them in the project's /assets directory.

Trying to open a file with this path...

/mnt/sdcard/cat/bird/flowers.xml

...means the AssetManager will try to look for a packaged file which you originally placed in your project with the following path...

/assets/mnt/sdcard/cat/bird/flowers.xml
网友答案:

Make sure you have the following permission. (may not be needed for just reading). Also on some devices reading from sdcard has been restricted.

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

Also do NOT use hardcoded paths, use the following to get external storage path.

String path = Environment.getExternalStorageDirectory() + "/cat/bird/flowers.xml";
网友答案:

The getAssets() is generally used to get access to files in the assets folder in your project. You should use something like:

InputStream inputStream = new FileInputStream("//sdcard//cat//bird//flowers.xml");
分享给朋友:
您可能感兴趣的文章:
随机阅读: