imageView中的图像加载变为黑色

时间:2017-01-10 17:17:26

标签: android bitmap imageview uri

所以我从厨房加载图像并显示到ImageView,图像变成黑色。但不是所有的图像,只是一定的。为什么会这样?

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

        case RESULT_LOAD_IMAGE:
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                imageUri = data.getData();
                imageView.setImageURI(imageUri);
            }
            break;
         }
     }

然后我尝试了这种方法。图像显示但看起来很模糊。

 imageView.setImageBitmap(decodeUri(imageUri));

 private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);
    }

2 个答案:

答案 0 :(得分:2)

是的,当Bitmap无法加载时会发生这种情况,因为图片的高度和宽度较大,为2048*2048。通过相机拍摄的图像通常很大,因此,最好调整图像大小。

以下只是裁剪图像的示例,如果尺寸较大,并且不考虑图像比率

public class InputImageCompressor extends Activity{

public static void compressInputImage(Intent data, Context context, ImageView newIV)
{
    Bitmap bitmap;
    Uri inputImageData = data.getData();
    try
    {
        Bitmap bitmapInputImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), inputImageData);
        if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() > 2048)
        {
            bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
            newIV.setImageBitmap(bitmap);
        }
        else if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() < 2048)
        {
            bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1920, 1200, true);
            newIV.setImageBitmap(bitmap);
        }
        else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() > 2048)
        {
            bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
            newIV.setImageBitmap(bitmap);
        }
        else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() < 2048)
        {
            bitmap = Bitmap.createScaledBitmap(bitmapInputImage, bitmapInputImage.getWidth(), bitmapInputImage.getHeight(), true);
            newIV.setImageBitmap(bitmap);
        }
    } catch (Exception e)
    {
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }
   }
}

onActivityResult中将其用作:

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

    case RESULT_LOAD_IMAGE:
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
             InputImageCompressor.compressInputImage(data, your_activity_context.this, imageView);
        }
        break;
     }
 }

答案 1 :(得分:1)

代码看起来还不错,你看到模糊图像的事实也证实了这一点。

我认为REQUIRED_SIZE对于您正在尝试的设备来说太小了。对于初学者,请尝试将REQUIRED_SIZE设置为更大的值,例如350,看看它是否提高了图像质量。如果是,则使用REQUIRED_SIZE = SCREEN_WIDTH

而不是静态放置&#34;尺寸&#34;

要查找SCREEN_WIDTH,请在活动(或至少一个视图)中使用以下内容:

DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int SCREEN_WIDTH = displaymetrics.widthPixels;
相关问题