0

我正在关注链接以创建一个应用程序,该应用程序从解析中接收图像和文本并将其显示在列表视图中,它实际上是一个惰性列表。一切都很好,工作完美。但在某些智能手机(如 Nexus4)或某些 HTC 手机中,图像不会显示,而是可绘制的替代图像。LogCat 显示这些手机的以下异常,我不确定为什么会发生这种情况。如果有人能弄清楚,请。

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

在第 88 行

private Bitmap decodeFile(File f) {

这个方法开始。第 73 行是 OutputStream os = new FileOutputStream(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();
    }
4

1 回答 1

0

啊,我发现问题是。它实际上发生在 android 4.4 上。这需要 developer.android.com 中所述的许可。
在 Android 4.4 上运行时,您的应用无法读取外部存储上的共享文件,除非您的应用具有 READ_EXTERNAL_STORAGE 权限。也就是说,getExternalStoragePublicDirectory() 返回的目录中的文件在没有权限的情况下不再可以访问。但是,如果您只需要访问由 getExternalFilesDir() 提供的特定于应用程序的目录,则不需要 READ_EXTERNAL_STORAGE 权限
,我只需添加 READ_EXTERNAL_STORAGE 权限。

于 2015-04-29T07:55:40.543 回答