在Android中进行图像裁剪时出现空指针异常

时间:2014-08-06 06:08:07

标签: android nullpointerexception android-camera onactivityresult

我正在做一个Android应用程序,我需要裁剪捕获的图像并在屏幕上显示。但是得到NullPointerException,BitmapFactory.decodeFile(file.getPath(),o2);在onActivityResult中返回null。

图像捕获后没有显示裁剪屏幕,它直接移动到onActivityResult。

我的代码:

camera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(getTempFile(MainActivity.this)));
            intent.putExtra("index", 0);
            intent.putExtra("crop", "true");
            intent.putExtra("aspectX", 0);
            intent.putExtra("aspectY", 0);
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 150);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 1);
        }
    });

protected File getTempFile(MainActivity mainActivity) {
    boolean isSDPresent = android.os.Environment.getExternalStorageState()
            .equals(android.os.Environment.MEDIA_MOUNTED);
    File path = null;
    if (isSDPresent) {
        path = new File(Environment.getExternalStorageDirectory(),
                this.getPackageName());
    } else {
        ContextWrapper cw = new ContextWrapper(this);

        path = cw.getDir(this.getPackageName(), Context.MODE_PRIVATE);
    }
    if (!path.exists()) {
        path.mkdir();
    }
    String fileName = "image1.jpg";
    return new File(path, fileName);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bundle extras = data.getExtras();
    if(requestCode==1){
        final File file = getTempFile(MainActivity.this);
        o2.inSampleSize = 8;
        Bitmap photo = BitmapFactory.decodeFile(file.getPath(), o2); // getting null here
        image.setImageBitmap(photo);
    }
}

错误日志:

08-06 11:41:33.177: E/AndroidRuntime(28605): FATAL EXCEPTION: main
08-06 11:41:33.177: E/AndroidRuntime(28605): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.example.imagecapture/com.example.imagecapture.MainActivity}: java.lang.NullPointerException
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3135)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3178)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread.access$1100(ActivityThread.java:134)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.os.Looper.loop(Looper.java:154)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread.main(ActivityThread.java:4624)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at dalvik.system.NativeStart.main(Native Method)
08-06 11:41:33.177: E/AndroidRuntime(28605): Caused by: java.lang.NullPointerException
08-06 11:41:33.177: E/AndroidRuntime(28605):    at com.example.imagecapture.MainActivity.onActivityResult(MainActivity.java:99)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.Activity.dispatchActivityResult(Activity.java:4672)
08-06 11:41:33.177: E/AndroidRuntime(28605):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)

2 个答案:

答案 0 :(得分:0)

Android 5.1及更高版本中存在此问题。 此特定版本在裁剪后不发送任何数据,因此该包提供空指针异常。

我通过使用一些开源库解决了这个问题。

通过这些,它可能会有所帮助。 https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

答案 1 :(得分:0)

更改你的//指示输出X和Y //   intent.putExtra(“outputX”,200);   intent.putExtra(“outputY”,306);或(200)