发送失败结果ResultInfo {who = null,request = 2,result = 0,data = null} camera

时间:2014-08-29 09:37:32

标签: java android eclipse

这是相机代码

   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();

    }

}
}

并且日志在这里

[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.access$1100(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.access$1100(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)]

3 个答案:

答案 0 :(得分:5)

如果您在Kotlin上遇到错误,只需将数据意图标记为 @nullable 即可添加?在Intent类型声明之后。所以看起来应该是这样的:

data: Intent?.

答案 1 :(得分:2)

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

    }

答案 2 :(得分:1)

我希望这段代码有用..

  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

 @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;
  }