BitmapFactory.Options抛出nullPointerException

时间:2013-01-04 16:39:28

标签: android 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 个答案:

答案 0 :(得分:1)

Nick,BitmapFactory.decodeStream必须返回null,这意味着它无法解码流。可能是文件中的值很差或者您无法打开文件。检查'stream'是否为null。