0

我有以下代码尝试重新缩放位图:

  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)

我该如何解决我的问题?

4

1 回答 1

1

Nick,BitmapFactory.decodeStream 必须返回 null,这意味着它无法解码流。可能文件中的值不正确或者您无法打开文件。检查“流”是否为空。

于 2013-01-04T18:32:24.413 回答