android - how to cache an image from a remote site
Can anyone please provide me some example on how to save an image i fetch from websites into a cache. I had try to include the following function into my code and call it once i run the activity.
public void getRemoteImage(String imageUrl) { imageUrl = "http://marga.mobile9.com/download/thumb/295/sexylady7_xo6npovn.jpg"; URL aURL = null; URLConnection conn = null; Bitmap bmp = null; CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap()); if (cache_result == null) { try { aURL = new URL(imageUrl); conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); cache_result = new CacheManager.CacheResult(); CacheManager.saveCacheFile(imageUrl, cache_result); } catch (Exception e) { //return null; } } bmp = BitmapFactory.decodeStream(cache_result.getInputStream());*/ Toast.makeText(context,"It works", Toast.LENGTH_SHORT).show(); //return bmp; }
However, I got a nullPointerException. Can someone please help me with it as i'm quite new in android.
I got this from logcat :
06-17 19:07:51.291: ERROR/AndroidRuntime(464): Uncaught handler: thread main exiting due to uncaught exception
06-17 19:07:51.301: ERROR/AndroidRuntime(464): java.lang.RuntimeException: Unable to start activity ComponentInfo{mobile9.android.gallery/mobile9.android.gallery.GalleryWallpapers}: java.lang.NullPointerException
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2454)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.os.Looper.loop(Looper.java:123)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread.main(ActivityThread.java:4310)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at dalvik.system.NativeStart.main(Native Method)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): Caused by: java.lang.NullPointerException
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.webkit.CacheManager.getCacheFile(CacheManager.开发者_StackOverflowjava:296)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at mobile9.android.gallery.GalleryWallpapers.hahatest(GalleryWallpapers.java:558)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at mobile9.android.gallery.GalleryWallpapers.onCreate(GalleryWallpapers.java:132)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
06-17 19:07:51.301: ERROR/AndroidRuntime(464): ... 11 more
Do you use WebView? If you don't, I believe you can't use CacheManager. Refer to the following bug report for further details:
http://code.google.com/p/android/issues/detail?id=7222
精彩评论