Get image Uri + thumbnail of the picture shot with camera in Android

血红的双手。 提交于 2019-11-28 00:34:50

问题


I want to update an ImageView with a picture I make with the build-in Android camera. I use the following code:

void getPhoto() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, TAKE_PICTURE);
    }

After that I acquire the photo with:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PICTURE) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView photoView = (ImageView) findViewById(R.id.photoId);
            photoView.setImageBitmap(photo);
            }
        }

But with this code no matter what I do I only get a thumbnail of the photo I made - my question is how can I get the Uri of the photo just made in order to work not with the thumbnail but with the original image?

Ps. I actually need the thumbnail of the photo but I need the Uri of the original photo too.


回答1:


I put my own URI in the intent to tell it where to save, then you know where it is, not sure there is any other way. Create some fields for your file.

private String imagePath = "/sdcard/Camera/test.jpg";
private File originalFile;

Then initialize the file.

originalFile = new File(imagePath);

Now start the Camera app with the intent, passing the URI as an extra.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri outputFileUri = Uri.fromFile(originalFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, RESULT_CAPTURE_IMAGE); 

In the onActivityResult() extract the URI

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RESULT_CAPTURE_IMAGE && resultCode == Activity.RESULT_OK) {
        mBitmap = BitmapFactory.decodeFile(originalImagePath, BitmapFactoryOptions);  //set whatever bitmap options you need.

So you can now build a bitmap using createBitmap or use file path for whatever you need



来源:https://stackoverflow.com/questions/7257573/get-image-uri-thumbnail-of-the-picture-shot-with-camera-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!