我正在使用隐式意图拍照.我一直在遵循教程中概述的工作.我遇到的问题是没有添加到Intent的额外内容.这是我正在使用的代码:
I am using an implicit Intent to take a picture. I have been following the work outlined in this tutorial. The issue that I am having is that the extra added to the Intent is not being delivered. Here is the code I'm using:
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为空.这是代码:
When the onActivityResult method is fired, the Intent is empty. Here is that code:
@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); } }为什么意图是空的?我需要怎么做才能解决此问题?
Why is the intent empty? What do I need to do to fix this issue?
推荐答案
为什么意图是空的?
Why is the intent empty?
通过在ACTION_IMAGE_CAPTURE请求中包含EXTRA_OUTPUT来要求将其为空.引用文档:
Because you asked for it to be empty, by including EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request. Quoting the documentation:
调用方可以传递一个额外的EXTRA_OUTPUT来控制将图像写入何处.如果不存在EXTRA_OUTPUT,则会在Extra字段中将一个小型图像作为Bitmap对象返回.这对于只需要较小图像的应用程序很有用.如果存在EXTRA_OUTPUT,则将全尺寸图像写入EXTRA_OUTPUT的Uri值.
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
我该怎么做才能解决此问题?
What do I need to do to fix this issue?
要么:
-
摆脱EXTRA_OUTPUT(如果您想要缩略图大小的图像),或者
Get rid of EXTRA_OUTPUT (if you want a thumbnail-sized image), or
停止寻找"data"附加内容,并查看在EXTRA_OUTPUT
Stop looking for the "data" extra, and look in the location that you specified in EXTRA_OUTPUT
更多推荐
相机意图未添加额外内容
发布评论