Android : Download images from server and save the

2019-03-06 00:20发布

问题:

I have gone through following links

enter link description here

enter link description here

and i have followed the following tutorial to do cache the images after loading it from server

enter link description here

and my code segment is :

private Bitmap getBitmap(String url) {
//      PhotoToLoad photoToLoad = new PhotoToLoad(url, new ImageView(a));
//      String filename = photoToLoad.url;
        //String filename = url;
        String filename = String.valueOf(url.hashCode());
        Log.v("TAG FILE :", filename);
        File f = new File(cacheDir, filename);
        // Is the bitmap in our cache?
        Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
        if (bitmap != null)
            return bitmap;
        else {
            // Nope, have to download it
            try {
                bitmap = BitmapFactory.decodeStream(new URL(url)
                        .openConnection().getInputStream());
                // save bitmap to cache for later
                writeFile(bitmap, f);
                return bitmap;
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
                Log.v("FILE NOT FOUND", "FILE NOT FOUND");
                return null;
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                return null;
            }
        }
    }

    private void writeFile(Bitmap bmp, File f) {
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
            }
        }
    }

I am getting FileNotFoundException :

11-12 15:19:25.495: VERBOSE/cacheDir(266): /sdcard/data/BalajeeBazaar

11-12 15:19:26.035: VERBOSE/TAG FILE :(266): -951166081

11-12 15:19:26.315: WARN/System.err(266): java.io.FileNotFoundException: /sdcard/data/BalajeeBazaar/-951166081

11-12 15:19:26.315: WARN/System.err(266):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:231)

11-12 15:19:26.315: WARN/System.err(266):     at java.io.FileOutputStream.<init>(FileOutputStream.java:96)

11-12 15:19:26.315: WARN/System.err(266):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.writeFile(CacheImages.java:160)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.getBitmap(CacheImages.java:142)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.access$0(CacheImages.java:125)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:77)

11-12 15:19:26.325: WARN/dalvikvm(266): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)

11-12 15:19:26.325: ERROR/AndroidRuntime(266): Uncaught handler: thread Thread-10 exiting due to uncaught exception

11-12 15:19:26.325: ERROR/AndroidRuntime(266): java.lang.ClassCastException: android.graphics.Bitmap

11-12 15:19:26.325: ERROR/AndroidRuntime(266):     at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:79)

Please guide me how to resolve this ?

Thanks~

回答1:

Are you properly defining the directory name?

Maybe you didn't declare the directory name properly...

So, can you please check that it's all right ??

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

Check your class and make sure your path is right or anything else and tell me after.



回答2:

In the stack trace it shows a file not found exception. I've seen this happen when the file folder doesn't exist. did you create it first. alternatively, you can use the cache or external folder mentioned above.