相机意图结果问题 [英] Camera Intent result woes

查看:25
本文介绍了相机意图结果问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动内置摄像头来拍照,这张照片的名称由启动摄像头的活动指定.(代码如下)

I am attempting to launch the built-in camera to take a picture, a picture that will have a name specified by the activity launching the camera. (code below)

  1. 当相机返回时,onActivityResult() 直接进入resultCode == Activity.RESULT_CANCELED.对此和解决方案的任何解释将不胜感激.

  1. When the camera returns, onActivityResult() goes straight to resultCode == Activity.RESULT_CANCELED. Any explanation for this and solutions would be greatly appreciated.

相机确实拍摄了图像,我可以使用文件查看器在我的 sdcard 中看到它,但它的名字是来自相机的股票.我怎样才能使这张拍摄的图像的名称成为活动提供的名称?

The camera indeed does take the image, I can see it in my sdcard with a file viewer, but its name is the stock one from the camera. How can I get the name of this taken image to be the one supplied by the activity?

相机意图代码

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File("Team image.jpg");
camera.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
camera.putExtra(MediaStore.Images.Media.TITLE, "Team image");
        startActivityForResult(camera, PICTURE_RESULT);

活动结果代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){

    if(requestCode == PICTURE_RESULT){
        if(resultCode == Activity.RESULT_OK) {
            if(data!=null){
                Bitmap image = BitmapFactory.decodeFile(data.getExtras().get(MediaStore.Images.Media.TITLE).toString());
                grid.add(image);            
                images.addItem(image);
            }
            if(data==null){
                Toast.makeText(Team_Viewer.this, "no data.", Toast.LENGTH_SHORT).show();
            }
        }
        else if(resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(Team_Viewer.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
        }
}
}

推荐答案

这两个问题可能是相关的,与您创建传递给相机的文件引用的方式有关.如果要将图像文件保存到 SD 卡,则需要创建一个文件引用,其中包含该位置的完整路径,而不仅仅是文件名.例如,此代码会将图像文件保存在 SD 卡根目录中:

The two issues are likely related, having to do with the way you are creating the file reference that passes to the camera. If you want your image file to save to the SD Card, you need to create a file reference that includes a full-path to that location, not just a filename. For example, this code would save the image file on the SD card root:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(Environment.getExternalStorageDirectory(),"TeamImage.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));

startActivityForResult(camera, PICTURE_RESULT);

我还将您的文件名更改为不包含空格;只是因为我不确定相机应用程序也不会在那块上爆炸.由于相机在尝试打开和写入您的文件位置时感到困惑,这可能就是您总是返回 RESULT_CANCELED 的原因.您在此处不需要 WRITE_EXTERNAL_STORAGE 权限,因为相机应用正在访问 SD 卡.

I also changed your filename to not include a space; only because I'm not certain that the Camera application won't blow up on that piece also. Since the Camera is getting confused trying to open and write to your file location, that is likely why you always return with RESULT_CANCELED. You don't need the WRITE_EXTERNAL_STORAGE permission here, since the Camera app is doing the SD Card access.

另外一个注意事项:我不相信其他 MediaStore extras 可以通过这个 Intent 传递.通常,如果您希望将元数据附加到您的图像,您必须在将图像保存到磁盘之前将带有该元数据的 Uri 引用插入到 MediaStore ContentProvider 中.

One more note: I don't believe other MediaStore extras can be passed with this Intent. Typically, if you want metadata to be attached to your image, you have to insert the Uri reference with that metadata into the MediaStore ContentProvider prior to saving the image to disk.

希望有帮助!

这篇关于相机意图结果问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆