【问题标题】:Null Pointer Exception on Bitmap.createBitmap in AndroidAndroid 中 Bitmap.createBitmap 上的空指针异常
【发布时间】:2015-08-01 10:32:23
【问题描述】:

我正在开发一个相机应用程序,我的问题是,在某些设备上,相机拍摄的图像正在旋转,当将其设置为 imageview 时,我已经搜索了解决方案,并且我从中找到了解决方案链接。camera intent auto rotate to 90 degree。我的问题是当我调用生成旋转位图图像的方法时,它在以下行显示 NPE

  Bitmap rotatedBitmap = Bitmap.createBitmap( bitencoded, 0, 0,  bitencoded.getWidth(),
            bitencoded.getHeight(), matrix, true);

完整代码

 private Bitmap rotateCam()
{
    Matrix matrix = new Matrix();
    matrix.postRotate(getImageOrientation(picturePath));

    Bitmap rotatedBitmap = Bitmap.createBitmap( bitencoded, 0, 0,  bitencoded.getWidth(),
            bitencoded.getHeight(), matrix, true);
    imageView.setImageBitmap(rotatedBitmap);
    return rotatedBitmap;
}

LogCat

java.lang.NullPointerException
        at project1.me.com.update.ImageUploadActivity.rotateCam(ImageUploadActivity.java:235)
        at project1.me.com.update.ImageUploadActivity.onCreate(ImageUploadActivity.java:182)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)

创建

 byte[] data = picturePath.getBytes("UTF-8");
          String base64PicPath = Base64.encodeToString(data, Base64.DEFAULT);

          bitencoded=  stringToBitMap(base64PicPath);
          try
          {
              rotateCam();
              Log.e(" Rotated", "Method  called");
          }
          catch(Exception e)
          {
            Log.e("Not Rotated","Method not called");
            e.printStackTrace();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }

getImageOrientation()

 public static int getImageOrientation(String imagePath){
    int rotate = 0;
    try {

        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(
                imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rotate;
}

我是一个初学者,我找不到 NPE 的理由。任何人都可以帮助我吗?提前谢谢。

【问题讨论】:

  • bitencoded 为空
  • 我已将图像路径转换为位图,并将该位图设置为此方法,但下一行仍为 NPE。您能帮忙吗?
  • picturePath 是什么?
  • 位图在sdcard上的文件路径?
  • 是的,图片的文件路径。

标签: android bitmap android-imageview android-orientation


【解决方案1】:

使用此方法转换位图。

 private Bitmap decodeFile(String filePath) {

    // Decode image size
    try {

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(filePath, o);
        final int REQUIRED_SIZE = 1024;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        o.inJustDecodeBounds = false;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bitmap = BitmapFactory.decodeFile(filePath, o2);

    }
    catch (Exception e) {
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
   return bitmap;
}

和 onCreate():

bitmap = decodeFile(picturePath);
rotateCam();

在rotateCam()中设置ImageView:

public void rotateCam() {
    Matrix matrix = new Matrix();
    matrix.postRotate(getImageOrientation(picturePath));
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
        bitmap.getWidth(),
        bitmap.getHeight(), matrix, true);
    imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(rotatedBitmap);
 }

【讨论】:

    【解决方案2】:
    Bitmap bitencoded=null;
    bitencode.getWidth();
    

    您正在对位编码的空引用调用 getWidth() 方法。 应该这样做,

    Bitmap bitencode = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
    

    那么,

    bitencode.getWidth();
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多