how to cache image fetch from website

Put your problem here if it does not fit any of the other categories.

how to cache image fetch from website

Postby lynnooi » Fri Jun 11, 2010 10:13 am

Hi, I'm quite new in android and I need to put the image i get from a website into the cache. After searching over the Internet, I still can't get it done. Can anyone provide me some guides or example on how I can do it? I have tried writing a simple function for it but it doesn't work. It has some nullPointerException in the logcat.

Here's the function i wrote:

Code: Select all
    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;
    }



Here's the error i got in logcat:
06-11 21:14:05.449: ERROR/AndroidRuntime(636): Uncaught handler: thread main exiting due to uncaught exception
06-11 21:14:05.469: ERROR/AndroidRuntime(636): java.lang.RuntimeException: Unable to start activity ComponentInfo{mobile9.android.gallery/mobile9.android.gallery.GalleryWallpapers}: java.lang.NullPointerException
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2454)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.os.Looper.loop(Looper.java:123)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread.main(ActivityThread.java:4310)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at java.lang.reflect.Method.invoke(Method.java:521)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at dalvik.system.NativeStart.main(Native Method)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): Caused by: java.lang.NullPointerException
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.widget.Toast.<init>(Toast.java:89)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.widget.Toast.makeText(Toast.java:231)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at mobile9.android.gallery.GalleryWallpapers.getRemoteImage(GalleryWallpapers.java:569)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at mobile9.android.gallery.GalleryWallpapers.onCreate(GalleryWallpapers.java:132)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
06-11 21:14:05.469: ERROR/AndroidRuntime(636): ... 11 more
lynnooi
Freshman
Freshman
 
Posts: 2
Joined: Mon Dec 21, 2009 9:32 am

Top

Re: how to cache image fetch from website

Postby fbarnard » Tue Jun 15, 2010 3:00 pm

Hi

Your exception is at Toast.makeText(null reference exception). Make sure context is not null
fbarnard
Developer
Developer
 
Posts: 44
Joined: Tue Jun 15, 2010 12:31 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 9 guests