如何拍一张照片,将其保存并获得照片的Andr​​oid [英] How to take a photo, save it and get the photo in Android

查看:131
本文介绍了如何拍一张照片,将其保存并获得照片的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的简单的例子来拍摄照片,并使用URI保存和retrive照片进行图像处理, 我试过很多例如code,但他们都不顺利。

i've been searching the simple example to take a photo, and save it using URI and retrive the photo for image processing , i've tried lot of example code, but none of them went smoothly.

有没有人有这个例子code?

is there anyone have the example code?

太感谢你了。

推荐答案

像这样定义一个变量

protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;

使用的code从Android的呼叫相机。

Use the code for calling camera from android.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" +        
                        String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

和在类中调用这个覆盖onActivityResult功能,并在下面输入code。

and in the class calling this override the onActivityResult function and enter the code below.

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

    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

            //use imageUri here to access the image

            Bundle extras = data.getExtras();

            Log.e("URI",imageUri.toString());

            Bitmap bmp = (Bitmap) extras.get("data");

            // here you will get the image as bitmap


        } 
          else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
           } 
         }


    }

这篇关于如何拍一张照片,将其保存并获得照片的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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