如何以相同的意图从相机获取完整尺寸的图片和缩略图 [英] How to get full size picture and thumbnail from Camera in the same intent

查看:176
本文介绍了如何以相同的意图从相机获取完整尺寸的图片和缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直需要解决此问题.我已经在这个社区中搜索并测试了许多解决方案,但是任何人都可以为我提供帮助.

I've been needing to get a solution for this problem. I've already searched and tested many solutions from this community but anyone was fit for helping me.

我有两个活动,第一个活动拍摄一张照片并将其发送到另一个活动,该活动具有一个ImageView来接收该活动(直到这里我遇到问题)和一个查询以在数据库中插入文件路径(做这最后一部分就很好了.

I have two activities, the first one takes a picture and sends it to another which has an ImageView to receive that (until here i'm getting problems) and a query to insert the file path in the database (the code which do this last part is well).

我认为它对于View加载图像低分辨率作为缩略图更好.因此,要保存到数据库中,我想从完整尺寸的图片中获取整个路径.

I guess its better for the View load an Image Low Resolution as a Thumbnail. Therefore, to save into the database i wanna get the whole path from a full size picture.

图像是否必须位于特定目录内,我可以选择给函数指定参数的名称.

Whether possible the images must to be inside a specific directory that i can choose the name given parameters to the function.

也许任何人也知道如何自动设置相机的最小分辨率.

Perhaps anyone also knows how to set the smallest resolution from the camera automatically.

推荐答案

使用camera意向获取完整图片和缩略图的示例

Example of get full size picture and thumbnail with camera intent

首先声明要拍照的按钮...

first declare a button to take a picture...

Button takePhoto = (Button) findViewById(R.id.btn_takePhoto);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 1);
            }
        });

然后在onActivityResult中,我们正在获取数据...

Then in onActivityResult we are getting the data...

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && resultCode == Activity.RESULT_OK){

                //path from full size image
                Cursor cursor = MyActivity.this.getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{
                                MediaStore.Images.Media.DATA,
                                MediaStore.Images.Media.DATE_ADDED,
                                MediaStore.Images.ImageColumns.ORIENTATION
                        },
                        MediaStore.Images.Media.DATE_ADDED,
                        null,
                        "date_added DESC");

                Bitmap fullsize;
                if (cursor != null && cursor.moveToFirst()) {
                    Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                    String photoPath = uri.toString();
                    cursor.close();
                    if (photoPath != null) {
                        System.out.println("path: "+photoPath); //path from image full size
                        fullsize = decodeSampledBitmap(photoPath);//here is the bitmap of image full size
                    }
                }

                // do whatever you want with the Bitmap .... fullsize


                // thumbnail
                Bitmap thumbnail;
                thumbnail = (Bitmap) data.getExtras().get("data");
                // do whatever you want with the Bitmap .... thumbnail
            }
        }

对于内存泄漏,请使用此功能

For memory leak use this functions

private int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    } 



private Bitmap decodeSampledBitmap(String pathName,
                                   int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pathName, options);
}

//I added this to have a good approximation of the screen size:
private Bitmap decodeSampledBitmap(String pathName) {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    return decodeSampledBitmap(pathName, width, height);
}

注意: encodeSampledBitmap是一种用于在内存泄漏时提高性能的函数,例如当您收到OOM(内存不足)....

Note: decodeSampledBitmap is a function to better performance in memory leak, like when you get OOM(out of memory)....

有效地加载大型位图

这篇关于如何以相同的意图从相机获取完整尺寸的图片和缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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