imageView中的图像加载变为黑色 [英] Image load in imageView from gallery become black
本文介绍了imageView中的图像加载变为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我从厨房加载图像并显示到 ImageView
,图像变为黑色。但不是所有的图像,只是一定的。为什么会发生这种情况?
So I load image from galley and display into ImageView
, the image turn to black. But not all images, just a certain. Why would this happen?
@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;
}
}
然后我尝试了这种方法。图像显示但看起来很模糊。
Then I tried this method. The image show but look blurry.
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);
}
推荐答案
是的,它发生在<$无法加载c $ c>位图,因为图像的高度和宽度较大, 2048 * 2048
。通过相机拍摄的图像通常很大,因此,最好调整图像大小。
Yes it happens when Bitmap
can't be loaded because the height and width of the image is larger in size which is 2048*2048
. Images captured through camera are usually large in size so, It is good practice to resize the image.
以下只是裁剪图像的示例,并没有照顾图像比率
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();
}
}
}
在<$中使用此项c $ c> onActivityResult as:
@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;
}
}
这篇关于imageView中的图像加载变为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文