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);
}
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.
Below is just an example to crop image if large in size,and does not take care of image ratio
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();
}
}
}
Use this in your 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;
}
}
The code looks alright, the fact that you are seeing a blurry image sort of confirms that too.
I think the REQUIRED_SIZE is too small for the device you are trying. For starters, try setting the REQUIRED_SIZE to something bigger, e.g. 350 and see if it improves the image quality. If it does, then instead of statically putting a "size", use REQUIRED_SIZE = SCREEN_WIDTH
To find SCREEN_WIDTH, use the following, from within an Activity (or at least a view):
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int SCREEN_WIDTH = displaymetrics.widthPixels;