从设备库调整上传的照片大小以在ImageView中显示它

时间:2013-10-17 16:19:42

标签: android

我正在实施一个Android程序,允许用户将照片从他们的设备库上传到ImageView。将其保存在云端。我的代码适用于小照片,但更大的照片导致应用程序停止。我收到了这个错误:

  

位图太大而无法上传到纹理中(4128x2322,max = 4096x4096)

我尝试调整上传的照片大小,然后使用之前问题的建议显示,但它们无效。我不确定我的代码有什么问题。

任何帮助将不胜感激。这是我上次尝试的代码:

{
    // omitted code segment from onCreate...
    browseButton = ((Button) findViewById(R.id.browse_button));
    browseButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
            }
        }
    );
}
//end on create

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
            // Convert it to byte
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Compress image to lower quality scale 1 - 100
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            image = stream.toByteArray();
            bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
            Bitmap toyImageScaled = Bitmap.createScaledBitmap(bitmap, 200, 200
            * bitmap.getHeight() / bitmap.getWidth(), false);
            // Override Android default landscape orientation and save portrait
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap rotatedScaledToyImage = Bitmap.createBitmap(toyImageScaled, 0,
            0, toyImageScaled.getWidth(), toyImageScaled.getHeight(),
            matrix, true);
            toyPreview.setImageBitmap(bitmap);
        }
    }
}

public String getPath(Uri uri)
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

2 个答案:

答案 0 :(得分:1)

您仍然会在以下位置展示大图:

toyPreview.setImageBitmap(bitmap);

您应该展示缩放后的图片,toyImageScaledrotatedScaledToyImage

答案 1 :(得分:0)

我认为您应该使用文档的示例,并使用InSampleSize。

请参阅:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

来自DOCS:

public static 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) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;

}