eclipse 未能传递结果 ResultInfo{who=null, request=2, result=0, data=null} 相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25565568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:55:29  来源:igfitidea点击:

Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} camera

javaandroideclipse

提问by Guru Charan

here is the camera code

这是相机代码

   private void selectImage(){

 final  int Camera_CPTURE = 1;

try {
    Intent capture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture,Camera_CPTURE);
}catch (ActivityNotFoundException e)
{
    String error ="opps ur devices doesn't support capturing";
    Toast toast = Toast.makeText(this,error,Toast.LENGTH_SHORT);
    toast.show();
}
}




protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (resultCode == RESULT_OK) {
        picUri = data.getData();
        performCrop();  }
    else if(requestCode == PIC_CROP)
    {
        Bundle extra = data.getExtras();
        Bitmap thePic = extra.getParcelable("data");
        ImageView picView = (ImageView)findViewById(R.id.imageView);
        picView.setImageBitmap(thePic);
    }

    }

private void performCrop() {
    try {
       Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri,"image/*");
        cropIntent.putExtra("crop","true");
        cropIntent.putExtra("aspectX",1);
        cropIntent.putExtra("aspectY",1);
        cropIntent.putExtra("outputX",200);
        cropIntent.putExtra("outputY",200);
        cropIntent.putExtra("return-data",true);
        startActivityForResult(cropIntent,PIC_CROP);

    }catch (ActivityNotFoundException e)
    {
        String error ="opps ur devices doesn't support capturing";
        Toast toast = Toast.makeText(this,error,Toast.LENGTH_SHORT);
        toast.show();
    }
}





@Override
public void onClick(View view) {
    if(view.getId()==R.id.button){
        selectImage();

    }

}
}

and the log is here

日志在这里

[java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, 

result=0, data=null} to activity 

{com.gurucharan.studentdatabase/com.gurucharan.studentdatabase.MyActivity}:                

java.lang.NullPointerException

at android.app.ActivityThread.deliverResults(ActivityThread.java:3007)

at android.app.ActivityThread.handleSendResult(ActivityThread.java:3050)

at android.app.ActivityThread.access00(ActivityThread.java:127)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:4448)

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:823)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)

at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NullPointerException

at com.gurucharan.studentdatabase.MyActivity.onActivityResult(MyActivity.java:61)

at android.app.Activity.dispatchActivityResult(Activity.java:4649)

at android.app.ActivityThread.deliverResults(ActivityThread.java:3003)
?
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3050)
?
at android.app.ActivityThread.access00(ActivityThread.java:127)
?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
?
at android.os.Handler.dispatchMessage(Handler.java:99)
?
at android.os.Looper.loop(Looper.java:137)
?
at android.app.ActivityThread.main(ActivityThread.java:4448)
?
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:823)
?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
?
at dalvik.system.NativeStart.main(Native Method)

08-29 14:40:13.503    1819-2133/? E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!

08-29 14:40:13.503    1819-2133/? E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!

08-29 14:40:13.503    2053-2053/? E/JavaBinder﹕ !!! FAILED BINDER TRANSACTION !!!

08-29 14:40:14.274  10409-10409/? E/wpa_supplicant﹕ RX ctrl_iface - hexdump_ascii(len=11)]

回答by Madi

If u get error on Kotlin just mark the dataintent as @nullableby adding a ? after the Intent type declaration. So it should look like so:

如果妳得到科特林错误只是标记数据意图作为@nullable通过增加?在 Intent 类型声明之后。所以它应该是这样的:

data: Intent?.

回答by Naveen Tamrakar

if(requestCode==Camera_CPTURE && resultCode=RESULT_OK && data!=null)
    {

    }

回答by Rakesh Rangani

i hope this code usefull..

我希望这个代码有用..

  private String selectedImagePath = "";
  final private int PICK_IMAGE = 1;
  final private int CAPTURE_IMAGE = 2;
  private String imgPath;

private void selectImage() {

AlertDialog.Builder builder = new AlertDialog.Builder(
        ProfileActivity.this);
// builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Take a Photo",
        "Choose from Gallery" },
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case 0:
                    Intent intent1 = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    intent1.putExtra(MediaStore.EXTRA_OUTPUT,
                            setImageUri());
                    startActivityForResult(intent1, CAPTURE_IMAGE);
                    break;
                case 1:
                    // GET IMAGE FROM THE GALLERY
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(
                            Intent.createChooser(intent, ""),
                            PICK_IMAGE);
                    break;
                default:
                    break;
                }
            }
        });
builder.show();
}


public Uri setImageUri() {

File file = new File(Environment.getExternalStorageDirectory(), "image" + new     Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}


 public String getImagePath() {
     return imgPath;
}

onActivityForResultSetMethod

onActivityForResultSetMethod

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode != Activity.RESULT_CANCELED) {
 if (requestCode == PICK_IMAGE) {
    selectedImagePath = getAbsolutePath(data.getData());
    System.out.println("path" + selectedImagePath);
    imageView.setImageBitmap(decodeFile(selectedImagePath));

} else if (requestCode == CAPTURE_IMAGE) {
    selectedImagePath = getImagePath();
    System.out.println("path" + selectedImagePath);
    imageView.setImageBitmap(decodeFile(selectedImagePath));


    } else {
     super.onActivityResult(requestCode, resultCode, data);
   }
  }
 }


 public Bitmap decodeFile(String path) {
 try {
      // Decode image size
      BitmapFactory.Options o = new BitmapFactory.Options();
       o.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, o);
     // The new size we want to scale to
      final int REQUIRED_SIZE = 70;

      // Find the correct scale value. It should be the power of
     // 2.
      int scale = 1;
     while (o.outWidth / scale / 2 >= REQUIRED_SIZE
          && o.outHeight / scale / 2 >= REQUIRED_SIZE)
       scale *= 2;

      // Decode with inSampleSize
     BitmapFactory.Options o2 = new BitmapFactory.Options();
      o2.inSampleSize = scale;
     return BitmapFactory.decodeFile(path, o2);
   } catch (Throwable e) {
     e.printStackTrace();
  }
   return null;
  }


 public String getAbsolutePath(Uri uri) {
 String[] projection = { MediaColumns.DATA };

   Cursor cursor = managedQuery(uri, projection, null, null, null);
   if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
   } else
     return null;
  }