简体   繁体   中英

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)

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.

Try the code below and handle errors at onLoadingFailed():

L.writeLogs(false);    

imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(photo_url, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                   imageView.setImageBitmap(loadedImage);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    super.onLoadingFailed(imageUri, view, failReason);
                    MyLogger.log(TAG, "Handle the error here");
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM