使用ACTION_IM​​AGE_CAPTURE进行拍照和setImageBitmap显示它 [英] using ACTION_IMAGE_CAPTURE to take picture and setImageBitmap to display it

查看:146
本文介绍了使用ACTION_IM​​AGE_CAPTURE进行拍照和setImageBitmap显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改我更新了code,以反映这两个答案的建议,遗憾的是,现在我的应用程序强制关闭的变化。错误是在底部列出

这是整体我的相机/图片类这个类应该拍张照片,它显示在屏幕上,并让另一个(除导入 S)类有照片用作附件的路径字符串。
它需要的画面精致,它的电子邮件画面很好,但我无法弄清楚如何使它显示图片的拍摄后。我知道有在那里有些马虎code(像在的onActivityResult ),但除了

一切

 位图BM = BitmapFactory.de codeFILE(image_string);
imageview.setImageBitmap(BM);

伟大工程,所以我在寻找一个解决方案很感兴趣。您对如何清洁起来也非常欢迎其他任何意见,这只是排序从多个不同来源的步履蹒跚起来。

 公共类LooksLike扩展活动
    {
    按钮摄像头;
    意图意图;
    静态字符串image_string;
    ImageView的ImageView的;
    公共无效的onCreate(捆绑savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.lookslike);
        摄像头=(按钮)findViewById(R.id.camera);
        ImageView的=(ImageView的)findViewById(R.id.debris_view);
        camera.setOnClickListener(新View.OnClickListener()
            {
            公共无效的onClick(视图v)
                {
                照相();
                }
            });
        }    公共无效takePhoto()
        {
        意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile(本)));
        startActivityForResult(意向,1);
        位图BM = BitmapFactory.de codeFILE(image_string);
        imageview.setImageBitmap(BM);
        }    私人文件getTempFile(上下文的背景下)
        {
        文件路径=新的文件(Environment.getExternalStorageDirectory(),context.getPackageName());
        如果(!path.exists())
            path.mkdir();
        返回新的文件(路径,debris.jpg);
        }@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
    {
    如果(结果code == RESULT_OK)
        {
        开关(要求code)
            {
            情况1:
            最终文件文件= getTempFile(本);
            尝试
                {
                Media.getBitmap(getContentResolver(),Uri.fromFile(文件));
                image_string = Uri.fromFile(文件)的ToString();
                位图BM = Bitmap.createScaledBitmap(
                    BitmapFactory.de codeFILE(image_string)
                    getResources()。getDisplayMetrics()。widthPixels,
                    getResources()。getDisplayMetrics()。heightPixels,
                    真正);
                ImageView的=(ImageView的)findViewById(R.id.debris_view);
              imageview.setImageBitmap(BM);
                }
            赶上(FileNotFoundException异常E)
                {
                Toast.makeText(getApplicationContext(),文件未发现异常,Toast.LENGTH_SHORT).show();
                }
            赶上(IOException异常E)
                {
                Toast.makeText(getApplicationContext(),IOException异常,Toast.LENGTH_SHORT).show();
                }
            打破;
            }
        }
    }
}

此外,这里是我的xml文件:

 <合并的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    < ImageView的
        机器人:ID =@ + ID / debris_view
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:scaleType =centerCrop
        机器人:SRC =@绘制/图标
        />
    <按钮
        机器人:ID =@ + ID /摄像头
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =20dip
        机器人:layout_gravity =CENTER_HORIZONTAL |底
        机器人:填充=12dip
        机器人:背景=#AA000000
        机器人:文字颜色=#FFFFFFFF
        机器人:文字=你想采取一个新的碎片照片吗?
        />
< /合并>


解决方案

您需要移动这两条线成的onActivityResult():

 案例1:
    最终文件文件= getTempFile(本);
    尝试{
        Media.getBitmap(getContentResolver(),Uri.fromFile(文件));
        image_string = Uri.fromFile(文件)的ToString();
        位图BM = BitmapFactory.de codeFILE(image_string);
        imageview.setImageBitmap(BM);
    }赶上(FileNotFoundException异常五){
        ...

code takePhoto()将在 startActivityForResult立即执行()(即它不会等待整理称为活动)。处理结果必须以的onActivityResult()来完成

EDIT i updated the code to reflect changes suggested in both answers, unfortunately, now my app force closes. the error is listed at the bottom

this is my camera/picture class in its entirety (except for imports) this class is supposed to take a picture, display it to the screen, and let another class have the string path of the picture for use as an attachment. it takes the picture fine, it emails the picture fine, but i cannot figure out how to make it display the picture after its taken. i know there is some sloppy code in there (like in the the onActivityResult) but everything except

Bitmap bm = BitmapFactory.decodeFile(image_string);
imageview.setImageBitmap(bm);

works great, so I am very interested in finding a solution. any other advice you have on how to clean this up would also be more than welcome, this was just sort of hobbled together from multiple different sources.

public class LooksLike extends Activity
    {       
    Button camera;
    Intent intent;
    static String image_string;
    ImageView imageview;
    public void onCreate (Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lookslike);
        camera = (Button) findViewById(R.id.camera);
        imageview = (ImageView) findViewById(R.id.debris_view);
        camera.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View v)
                {
                takePhoto();
                }
            });
        }

    public void takePhoto()
        {
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
        startActivityForResult(intent, 1);
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
        }

    private File getTempFile(Context context)
        {
        File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
        if(!path.exists())
            path.mkdir();
        return new File(path, "debris.jpg");
        }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    if (resultCode == RESULT_OK)
        {
        switch(requestCode)
            {
            case 1:
            final File file = getTempFile(this);
            try
                {
                Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
                image_string = Uri.fromFile(file).toString();
                Bitmap bm = Bitmap.createScaledBitmap(
                    BitmapFactory.decodeFile(image_string),
                    getResources().getDisplayMetrics().widthPixels,
                    getResources().getDisplayMetrics().heightPixels, 
                    true);
                imageview = (ImageView) findViewById(R.id.debris_view);
              imageview.setImageBitmap(bm);
                }
            catch (FileNotFoundException e)
                {
                Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
                }
            catch (IOException e)
                {
                Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
                }
            break;
            }
        }
    }
}

also, here is my xml file:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView  
        android:id="@+id/debris_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/icon"
        />
    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="would you like to take a new debris picture?"
        />
</merge>

解决方案

You need move these 2 lines into onActivityResult():

case 1:
    final File file = getTempFile(this);
    try {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        image_string = Uri.fromFile(file).toString();
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
    } catch (FileNotFoundException e) {
        ...

Code in takePhoto() will be executed immediately after startActivityForResult() (i.e. it will not wait finishing called activity). Processing result must be done in onActivityResult().

这篇关于使用ACTION_IM​​AGE_CAPTURE进行拍照和setImageBitmap显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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