Android 相机意图:如何获得全尺寸照片? [英] Android Camera Intent: how to get full sized photo?

查看:17
本文介绍了Android 相机意图:如何获得全尺寸照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Intent 来启动相机:

I am using intent to launch camera:

Intent cameraIntent = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParent().startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

并使用:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
photoImage.setImageBitmap(thumbnail);
photoImage.setVisibility(View.VISIBLE);

但它只是一个缩略图,我如何获得完整的位图?我知道我可以使用自己的 Activity 并使用:

But it is only a thumbnail, how do I get the full bitmap? I know I can use my own Activity and use:

Camera.PictureCallback()

但是无论如何可以使用 Intent 来做到这一点?

But is there anyway to do it using Intent?

谢谢!

我也试过:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = data.getData();
    imageView.setImageURI(uri);
}

它适用于从图库中选择的照片,但对于相机意图,data.getData() 返回 null.

It works for photo selected from gallery, but for camera intent, data.getData() returns null.

推荐答案

要获得全尺寸的相机图像,您应该指向相机将图片保存在临时文件中,例如:

To get full sized camera image you should point camera to save picture in temporary file, like:

    private URI mImageUri;

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo;
    try
    {
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    }
    catch(Exception e)
    {
        Log.v(TAG, "Can't create file to take picture!");
        Toast.makeText(activity, "Please check SD card! Image shot is impossible!", 10000);
        return false;
    }
    mImageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    //start camera intent
    activity.startActivityForResult(this, intent, MenuShootImage);

private File createTemporaryFile(String part, String ext) throws Exception
{
    File tempDir= Environment.getExternalStorageDirectory();
    tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
    if(!tempDir.exists())
    {
        tempDir.mkdirs();
    }
    return File.createTempFile(part, ext, tempDir);
}

然后在图像捕获意图完成工作后 - 只需使用以下代码从 imageUri 抓取您的图片:

Then after image capture intent finished to work - just grab your picture from imageUri using following code:

public void grabImage(ImageView imageView)
{
    this.getContentResolver().notifyChange(mImageUri, null);
    ContentResolver cr = this.getContentResolver();
    Bitmap bitmap;
    try
    {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
        imageView.setImageBitmap(bitmap);
    }
    catch (Exception e)
    {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Failed to load", e);
    }
}


//called after camera intent finished
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    //MenuShootImage is user defined menu option to shoot image
    if(requestCode==MenuShootImage && resultCode==RESULT_OK) 
    {
       ImageView imageView;
       //... some code to inflate/create/find appropriate ImageView to place grabbed image
       this.grabImage(imageView);
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

PS 代码需要根据 Android M 中应用的新安全限制进行修改 - FileProvider: mImageUri 必须和 FileProvider

P.S. Code need to be revised with respect to new security restriction applied in Android M - FileProvider: mImageUri has to be packed with FileProvider

这篇关于Android 相机意图:如何获得全尺寸照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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