Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException: (No such file or directory)

Tags:

android

I am getting the following message in android LogCat

03-20 01:45:03.362: WARN/System.err(369): java.io.FileNotFoundException: /mnt/sdcard/LazyList/-2012431329 (No such file or directory)
03-20 01:45:03.362: WARN/System.err(369):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
03-20 01:45:03.372: WARN/System.err(369):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
03-20 01:45:03.382: WARN/System.err(369):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
03-20 01:45:03.382: WARN/System.err(369):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
03-20 01:45:03.392: WARN/System.err(369):     at com.ImageLoaders.ImageLoader.getBitmap(ImageLoader.java:86)

For downloading images in android emulator I have added internet permission in androidManifest.xml file, but looks like it is not working. I have also given 10MB space to android emulator as well.

any one guide me what could be the problem? thanks in advance.

like image 941
UMAR-MOBITSOLUTIONS Avatar asked Mar 19 '11 13:03

UMAR-MOBITSOLUTIONS


People also ask

How do I fix Java IO FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

What is Java IO FileNotFoundException?

Class FileNotFoundException Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.

What is FileOutputStream in Java?

FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object.

Is FileNotFoundException a runtime exception?

FileNotFoundException occurs at runtime so it is a checked exception, we can handle this exception by java code, and we have to take care of the code so that this exception doesn't occur.


2 Answers

I solved this problem adding permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 89
Jose Manuel Avatar answered Oct 19 '22 03:10

Jose Manuel


ImageLoader Constructor should be change to this.

public ImageLoader(Context context) {
    // Make the background thead low priority. This way it will not affect
    // the UI performance
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);

    // Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir = new File(context.getCacheDir(), "LazyList");
    else
        cacheDir = context.getCacheDir();
    if (!cacheDir.exists())
        cacheDir.mkdirs();
}

By default it is like this

cacheDir = new File(android.os.Environment.getDataDirectory(), "LazyList");

You should change it to

cacheDir = new File(context.getCacheDir(), "LazyList");

I solved my problem 100% in android 2.1.

like image 33
Chrishan Avatar answered Oct 19 '22 05:10

Chrishan