加载摄像机图像时,Android失败,提供结果resultinfo

时间:2015-10-15 22:08:39

标签: android android-camera

我试图在从相机捕获图像后加载图像,但是我得到了 "未能提供结果resultinfo"错误。以下是我收到错误时的值:

resultCode = -1

requestCode = 0

data = null

为什么Intent数据变为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);
}

1 个答案:

答案 0 :(得分:0)

  

为什么Intent数据变为空?

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