在某些android手机中解析listview filenotfound异常
我在下面的链接中创建了一个应用程序,它从解析中接收图像和文本,并在列表视图中显示出来,它实际上是一个惰性列表。一切都很好,工作得很完美。但在一些智能手机中,比如Nexus4或HTC手机,图像不会显示,而是显示drawable的替代图像。LogCat显示了这些手机的以下例外情况,我不知道为什么会发生这种情况。 如果有人能弄明白的话,请在某些android手机中解析listview filenotfound异常,android,filenotfoundexception,lazylist,Android,Filenotfoundexception,Lazylist,我在下面的链接中创建了一个应用程序,它从解析中接收图像和文本,并在列表视图中显示出来,它实际上是一个惰性列表。一切都很好,工作得很完美。但在一些智能手机中,比如Nexus4或HTC手机,图像不会显示,而是显示drawable的替代图像。LogCat显示了这些手机的以下例外情况,我不知道为什么会发生这种情况。 如果有人能弄明白的话,请 04-28 15:37:03.971: W/System.err(1434): java.io.FileNotFoundException: /storage/em
04-28 15:37:03.971: W/System.err(1434): java.io.FileNotFoundException: /storage/emulated/0/ParseListViewImgTxt/-2050135145: open failed: ENOENT (No such file or directory)
04-28 15:37:03.971: W/System.err(1434): at libcore.io.IoBridge.open(IoBridge.java:409)
04-28 15:37:03.971: W/System.err(1434): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-28 15:37:03.971: W/System.err(1434): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-28 15:37:03.971: W/System.err(1434): at co.howlabs.cafe.lahore.ImageLoader.getBitmap(ImageLoader.java:73)
04-28 15:37:03.971: W/System.err(1434): at co.howlabs.cafe.lahore.ImageLoader.access$0(ImageLoader.java:56)
04-28 15:37:03.971: W/System.err(1434): at co.howlabs.cafe.lahore.ImageLoader$PhotosLoader.run(ImageLoader.java:147)
04-28 15:37:03.971: W/System.err(1434): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
04-28 15:37:03.971: W/System.err(1434): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-28 15:37:03.971: W/System.err(1434): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-28 15:37:03.971: W/System.err(1434): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-28 15:37:03.971: W/System.err(1434): at java.lang.Thread.run(Thread.java:841)
04-28 15:37:03.971: W/System.err(1434): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-28 15:37:03.971: W/System.err(1434): at libcore.io.Posix.open(Native Method)
04-28 15:37:03.971: W/System.err(1434): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-28 15:37:03.971: W/System.err(1434): at libcore.io.IoBridge.open(IoBridge.java:393)
04-28 15:37:03.971: W/System.err(1434): ... 10 more
此方法启动。
第73行是
OutputStream os=新文件OutputStream(f)代码>
打开目录的代码
public FileCache(Context context) {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"ParseListViewImgTxt");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
啊,我想问题是。事实上,android 4.4就是这样。需要开发者在developer.android.com中声明的权限。
在Android 4.4上运行时,您的应用程序无法读取外部存储上的共享文件,除非您的应用程序具有读取外部存储权限。也就是说,如果没有权限,则无法再访问getExternalStoragePublicDirectory()返回的目录中的文件。但是,如果您只需要访问getExternalFilesDir()提供的特定于应用程序的目录,则不需要读取外部存储权限
我只是添加了READ\u EXTERNAL\u存储权限。尝试添加Log.d(“存储状态”,Environment.getExternalStorageState())
创建启动活动并在这些设备上共享此操作返回的内容。它返回“mounted”@internal_class7我认为您不应该在存储数据的位置使用正确的目录/storage/emulated/0/ParseListViewImgTxt/-2050135145。它应该是/storage/simulated/0/sdcard/ParseListViewImgTxt/-2050135145,但HTC手机上的/sdcard路径似乎有问题,LG nexus 4不太清楚。。。我使用LG G3和环境。getExternalStorageDirectory()+“/DirectoryName”适合我。你能分享你打开目录进行写和读的代码吗?它在nexus 4、摩托罗拉moto X和华为上引发了同样的异常,但在nexus 10、samasung glaxy和LG Optimus上运行良好。请检查编辑,我放置了打开目录的代码。@inner\u class7我找出问题并回答它。谢谢你让我这么想:)
public FileCache(Context context) {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"ParseListViewImgTxt");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}