Search code examples
javaandroidfilenotfoundexceptionuniversal-image-loader

How to handle java.io.FileNotFoundException while loading images from webservice in android


i have issues java.io.FileNotFoundException error in my application , in my aplication am using Android-Universal-Image-Loader,for loading images form web service, i know Filenotfound exceprion means that url is not valid, i need to hanlde this excepton can any one help me , how to handle this error.

public class TestApp extends Application {
    // private static final String TAG = TFEApplication.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.FIFO).build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }
}
public class MainActivity extends Activity {
    private ImageLoader mImageLoader;
    private DisplayImageOptions mDisplayImageoptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDisplayImageoptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher).cacheInMemory(true)
                .cacheOnDisc(true).considerExifParams(true).build();
        mImageLoader = ImageLoader.getInstance();
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onStart() {

        super.onStart();

        try{
        ImageView testView =(ImageView) findViewById(R.id.imageid);
        mImageLoader.displayImage("http://examle.com",testView, mDisplayImageoptions);
        }catch(Exception execException)
        {
            System.out.println(execException.getMessage());
        }
    }

}

Error am getting :

java.io.FileNotFoundException: http://exame.com/text.jpg
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
 com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:111)
    at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:77)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:319)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisc(LoadAndDisplayImageTask.java:298)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:241)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:141)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)

Solution

  • EDIT

    So after going through the source, it looks like this is a bug with the library. In LoadAndDisplayImageTask.java the first line of tryLoadBitmap() is File imageFile = getImageFileInDiscCache(). This is what is throwing the exception. getImageFileInDiscCache() needs to handle the IOException cleanly so the rest can continue on if the cache is not found. Unfortunately, I'm not seeing a real easy way to override this with the .jar. You could import the project from github and make the modification yourself.