2013-01-04 109 views
0

我有以下的代碼,我試着重新調整位圖:BitmapFactory.Options拋出NullPointerException

FileInputStream stream = new FileInputStream(new File(getCachePath(context), makeCacheFileName(uri))); 

      //Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      value = BitmapFactory.decodeStream(stream, null, o); 

      value = Bitmap.createScaledBitmap(
        value, // bitmap to resize 
        o.outWidth, // new width 
        o.outHeight, // new height 
        true); // bilinear filtering 

      stream.close(); 

日誌如下:

01-04 16:24:18.101: E/AndroidRuntime(27524): FATAL EXCEPTION: main 
    01-04 16:24:18.101: E/AndroidRuntime(27524): java.lang.NullPointerException 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:344) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.network.ImageThreadLoader$DiskCache.get(ImageThreadLoader.java:295) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.network.ImageThreadLoader.loadImageFromCache(ImageThreadLoader.java:185) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.products.ProductListAdapter.switchImages(ProductListAdapter.java:119) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.products.ProductListAdapter.access$0(ProductListAdapter.java:79) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.products.ProductListAdapter$1.run(ProductListAdapter.java:73) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at android.os.Handler.handleCallback(Handler.java:587) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at android.os.Handler.dispatchMessage(Handler.java:92) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at android.os.Looper.loop(Looper.java:130) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at android.app.ActivityThread.main(ActivityThread.java:3683) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at java.lang.reflect.Method.invokeNative(Native Method) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at java.lang.reflect.Method.invoke(Method.java:507) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    01-04 16:24:18.101: E/AndroidRuntime(27524): at dalvik.system.NativeStart.main(Native Method) 

我怎樣才能解決我的問題?

回答

1

Nick,BitmapFactory.decodeStream必須返回null,這意味着它無法解碼流。可能文件中的值很差或者無法打開文件。檢查'流'爲空。

相關問題