获取从图库中选择的图像的缩略图

时间:2013-08-19 00:56:47

标签: android android-gallery

我尝试了这段代码:

   if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            photoUri = capturedImagePath();
            Log.d("path to captured image", capturedImagePath().toString());
            Bitmap aBitmap = BitmapFactory.decodeFile(photoUri.toString());
            mProfilePic.setImageBitmap(aBitmap);
            mEncodedImageString = convertBitmapToString(aBitmap);
        Log.v("Base64 Image String : ", mEncodedImageString);

    } else if (requestCode == SELECT_FILE) {
        photoUri = data.getData();
        String[] filePathColumn = {
            MediaStore.Images.Media.DATA
        };
        Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null,
                null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap aBitmap = BitmapFactory.decodeFile(picturePath);
        mProfilePic.setImageBitmap(aBitmap);
        mEncodedImageString = convertBitmapToString(aBitmap);
        Log.v("Base64 Image String : ", mEncodedImageString);
    }
}

获取图像路径,但它给了我原始图像。我想要的是所选图像的缩略图。谁能帮助我实现同样的目标呢?

2 个答案:

答案 0 :(得分:1)

您可以轻松创建如下缩略图:

int thumbFactor = 4; // choose a power of 2
Bitmap thumb = Bitmap.createScaledBitmap(image, image.getWidth()/thumbFactor, image.getHeight()/thumbFactor, false);

请参阅Bitmap

答案 1 :(得分:1)

实际上你无法通过URI查询获取缩略图。因为Gallery会自动缓存缩略图。如果您需要缩略图,则必须自己解码原始图像。