Android摄像头意图创建两个文件 [英] Android Camera intent creating two files

查看:84
本文介绍了Android摄像头意图创建两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个程序,它需要的图片,然后显示它的缩略图。 使用仿真器时一切顺利,并丢弃按钮删除照片。 但是,一个真正的设备上的摄像头故意将图像保存在imageUri变量,第二个名为如果我刚打开了摄像头,并拍了照片本身等。

 私有静态最终诠释CAMERA_PIC_REQUEST = 1337;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.camera);

    //启动相机
    值=新ContentValues​​();
    values​​.put(MediaStore.Images.Media.TITLE,新画面);
    values​​.put(MediaStore.Images.Media.DESCRIPTION,从你的相机);
    。imageUri = getContentResolver()插入(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观);
    图像=(ImageView的)findViewById(R.id.ImageView01);
    意向意图=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
    startActivityForResult(意向,CAMERA_PIC_REQUEST);


    //保存图像按钮
    按钮保存=(按钮)findViewById(R.id.Button01);
    按钮,关闭=(按钮)findViewById(R.id.Button02);
}

@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAMERA_PIC_REQUEST和放大器;&安培;结果code == RESULT_OK){
        尝试{
            缩略图= MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);
            image.setImageBitmap(缩略图);
        }
        赶上(例外五){
            e.printStackTrace();
        }
    }
     其他{
         完();
     }
}

公共无效myClickHandler(查看视图){
    开关(view.getId()){
    案例R.id.Button01:
        完();
        打破;
    案例R.id.Button02:
        dicard();
    }
}

私人无效dicard(){
    。getContentResolver()删除(imageUri,NULL,NULL);
    完();
}
 

解决方案

有些Android手机存储原始照片中的画廊,只有在自己的位置的缩略图。这不要紧,你就与原来的要求是什么。我有两个不同的HTC手机做这件事,而其他品牌的转换不这样做。

我这解决了另一种方式。我跑在画廊的每个项目的查询和加载的BucketIDs到一个数组。我这样做的时候,我的应用程序启动相机应用。当相机应用的回报,我做出同样的查询(与项目最近添加的,以节省时间)。我比较这对我原来的列表,找到新的BucketID。接着,我比较这图象与I明确地设置为输出的文件的大小。如果它是更大的,我把它复制,替换我了。然后,我删除了文件,并从库中删除。

疼痛的你知道是什么!

我不得不再次改变这个,当我发现一个电话,没有保持独特的桶的ID ...查看我的链接后​​下面这个答案更多。

I am making a program that takes a picture and then shows it's thumbnail. When using the emulator all goes well and the discard button deletes the photo. But on a real device the camera intent saves the image at the imageUri variable and a second one that is named like if I had just opened up the camera and took a picture by itself.

private static final int CAMERA_PIC_REQUEST = 1337;  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    //start camera
    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    image = (ImageView) findViewById(R.id.ImageView01);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, CAMERA_PIC_REQUEST);


    //save the image buttons
    Button save = (Button) findViewById(R.id.Button01);
    Button close = (Button) findViewById(R.id.Button02);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
        try{
            thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
            image.setImageBitmap(thumbnail);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
     else{
         finish();
     }
}

public void myClickHandler(View view) {
    switch (view.getId()) {
    case R.id.Button01:
        finish();
        break;
    case R.id.Button02:
        dicard();
    }
}

private void dicard(){
    getContentResolver().delete(imageUri, null, null);
    finish();
}

解决方案

Some Android phones store the original photo in the gallery, and a thumbnail only in your location. It doesn't matter what you did with the original request. I have two different HTC phones doing it, and a slew of other brands not doing it.

I solved this another way. I ran a query of every item in the gallery and loaded the BucketIDs to an array. I do this when my app starts the camera app. When the camera app returns, I make the same query (with items recently added to save time). I compare this to my original list and find the new BucketID. Next, I compare the size of this image with the file I explicitly set as the output. If it's bigger, I copy it, replacing what I had. Then I delete the file and remove it from the gallery.

Pain in the you-know-what!

[EDIT] I had to change this around again when I discovered a phone that didn't keep unique bucket IDs... See my post in the link following this answer for more.

这篇关于Android摄像头意图创建两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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