相机意向的结果困境 [英] Camera Intent result woes

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

问题描述

所以。我试图启动内置相机拍摄照片,图像,将有由活动启动照相机指定一个名字。 (以下code)

  1. 当相机返回, onActivityResult()去直接结果code == Activity.RESULT_CANCELED 。这和解决方案的任何解释,将大大AP preciated。

  2. 相机的确的确需要形象,我可以看到它在我的SD卡与文件浏览器,但它的名字是股市一个来自相机。我怎样才能得到由活动之一提供的这个拍摄的图像的名字?

相机意图code

 意图摄像头=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
文件图像=新的文件(团队image.jpg文件);
camera.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(图片));
camera.putExtra(MediaStore.Images.Media.TITLE,团队形象);
        startActivityForResult(摄像头,PICTURE_RESULT);
 

activityresult code

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){

    如果(要求code == PICTURE_RESULT){
        如果(结果code == Activity.RESULT_OK){
            如果(数据!= NULL){
                位图图像= BitmapFactory.de codeFILE(data.getExtras()得到(MediaStore.Images.Media.TITLE)的ToString());
                grid.add(图像);
                images.addItem(图像);
            }
            如果(数据== NULL){
                Toast.makeText(Team_Viewer.this,没有数据。,Toast.LENGTH_SHORT).show();
            }
        }
        否则,如果(结果code == Activity.RESULT_CANCELED){
            Toast.makeText(Team_Viewer.this,图片不能采取Toast.LENGTH_SHORT).show();
        }
}
}
 

解决方案

这两个问题都有可能涉及,不必与正在创建传递到相机的文件引用的方式。如果你想让你的图像文件保存到SD卡,你需要创建一个文件引用,其中包括一个完整路径到该位置,不只是一个文件名。例如,该code将节省SD卡根目录下的图像文件:

 意图摄像头=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
文件图像=新的文件(Environment.getExternalStorageDirectory(),TeamImage.jpg);
camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(图片));

startActivityForResult(摄像头,PICTURE_RESULT);
 

我也改变了你的文件名不能包含空格;只是因为我不能确定的相机应用程序不会弄脏那块也。由于该相机是越来越困惑试图打开和写入文件的位置,这可能是为什么你总是用 RESULT_CANCELED 返回。你并不需要WRITE_EXTERNAL_STORAG​​E许可在这里,由于相机的应用程序在做SD卡存取。

还要说明一点:我不相信其他MediaStore群众演员可以用这个意图通过。通常情况下,如果你要的元数据附加到你的形象,你必须用插入元数据到MediaStore ContentProvider的前URI引用的图像保存到磁盘。

希望帮助!

So. 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天全站免登陆