相机意图结果困扰 [英] Camera Intent result woes

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

问题描述

我正在尝试启动内置相机拍摄照片,该照片将具有由启动相机的活动指定的名称。 (代码如下)


  1. 当相机返回时, onActivityResult()直接到 resultCode == Activity.RESULT_CANCELED 。任何解释和解决方案将非常感谢。


  2. 相机确实拍摄图片,我可以使用文件查看器在我的sdcard中看到它,但它的名字是从相机的。


相机意图代码

/ p>

  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);

活动结果代码

 code> @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) 。显示();
}
}
}


解决方案>

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

  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卡访问。



另一个注意事项:我不相信其他MediaStore演员可以通过这个意图。通常,如果要将元数据附加到映像,则必须在将映像保存到磁盘之前将具有该元数据的Uri引用插入MediaStore ContentProvider。



希望有所帮助!


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. When the camera returns, onActivityResult() goes straight to resultCode == Activity.RESULT_CANCELED. Any explanation for this and solutions would be greatly appreciated.

  2. 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?

Camera intent code

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);

activityresult code

@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();
        }
}
}

解决方案

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);

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.

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.

Hope that helps!

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

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