2015-10-15 91 views
0

Iam试图从摄像头获取图像后加载图像,但iam “发送结果resultinfo失败”错误。为什么意向数据即将为空Android加载摄像头图像时发生结果resultinfo失败

发送resultCode = -1

requestCode = 0

数据= NULL

:下面是值当我得到这个错误吗?这段代码有什么问题?

int REQUEST_CAMERA = 0 

private void selectImage() { 
    final CharSequence[] items = { "Take Photo", "Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileSettings.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { 

     if (items[item].equals("Take Photo")) 
     { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"))); 

      startActivityForResult(intent, REQUEST_CAMERA); 
     } 
     else if (items[item].equals("Cancel")) 
     { 
      dialog.dismiss(); 
     } 
    } 
    }); 

    builder.show(); 
} 


@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_CAMERA) 
      onCaptureImageResult(data); 
    } 
} 

private void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

    File destination = new File(Environment.getExternalStorageDirectory(), 
      System.currentTimeMillis() + ".jpg"); 

    FileOutputStream fo; 
    try { 
     destination.createNewFile(); 
     fo = new FileOutputStream(destination); 
     fo.write(bytes.toByteArray()); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    mainImage.setImageBitmap(thumbnail); 
} 

回答

0

why the Intent data is coming as null?

您提供EXTRA_OUTPUT。如果您这样做,则无需处理您的请求的相机应用程序通过Intent返回结果。毕竟,您不需要结果,因为您知道全尺寸图像的写入位置:您通过EXTRA_OUTPUT指示的位置。

+0

我们如何解决这个问题? – Abhi1988

+0

@ Abhi1988:停止尝试使用'Intent',并使用您在'EXTRA_OUTPUT'中提供的位置。 – CommonsWare

相关问题