我正在使用一个隐含的Intent来拍照.我一直在关注本教程中概述的工作.我遇到的问题是没有交付添加到Intent的额外内容.这是我正在使用的代码:
private void dispatchTakePictureIntent(Context context) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(this.getActivity()); } catch (IOException ex) { Log.e(TAG, "Error creating file: " + ex.toString()); //TODO: 2017/1/24 - Handle file not created AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Error") .setMessage(ex.toString()); final AlertDialog dialog = builder.create(); dialog.show(); } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(context, "com.example.myapp", photoFile); //THIS EXTRA IS NOT BEING ADDED TO THE INTENT takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); galleryAddPic(context, photoFile.getAbsolutePath()); } } }
onActivityResult
触发该方法时,该 方法Intent
为空.这是代码:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { //These extras are empty. I have used the debug tool, and there is nothing in here. Bundle extras = data.getExtras(); //extras is null imageBitmap = (Bitmap) extras.get("data"); previewImage.setImageBitmap(imageBitmap); } }
为什么意图是空的?我该怎么做才能解决这个问题?
为什么意图是空的?
因为您要求它是空的,包含EXTRA_OUTPUT
在您的ACTION_IMAGE_CAPTURE
请求中.引用文档:
调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置.如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象.这对于只需要小图像的应用程序非常有用.如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值.
我该怎么做才能解决这个问题?
或者:
摆脱EXTRA_OUTPUT
(如果你想要缩略图大小的图像),或
停止寻找"data"
额外的,并查看您指定的位置EXTRA_OUTPUT