0

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

位图太大,无法上传到纹理中(4128x2322,最大值=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);
}
4

2 回答 2

1

您仍在以下位置显示大图:

toyPreview.setImageBitmap(bitmap);

您应该显示缩放的图像,toyImageScaledrotatedScaledToyImage.

于 2013-10-17T16:34:35.123 回答
0

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

看到这个:http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html

来自文档:

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;

}

于 2013-10-17T16:30:45.967 回答