Search code examples
androidfilenotfoundexceptionlazylist

parse listview filenotfound exception in some android phones


I am following this link for creating an app which receive image and text from parse and show it in a listview, it's actuly a lazy list. Everything is fine a working perfectally. but In some of smart phones like Nexus4 or some HTC phones images don't show but alternative image from drawable. LogCat shows following exception for these phones, I am not sure why this is happening. If any one could figure it out please.

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

on line 88

private Bitmap decodeFile(File f) {

this method starts. and line 73 is OutputStream os = new FileOutputStream(f);

code for opening directory

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();
    }

Solution

  • Ah I figure out the problem is. its actully happened for android 4.4. which needs a permission as stated in developer.android.com.
    Your app can not read shared files on the external storage when running on Android 4.4, unless your app has the READ_EXTERNAL_STORAGE permission. That is, files within the directory returned by getExternalStoragePublicDirectory() are no longer accessible without the permission. However, if you need to access only your app-specific directories, provided by getExternalFilesDir(), then you do not need the READ_EXTERNAL_STORAGE permission
    I just add READ_EXTERNAL_STORAGE permission.