2

Problem occurs while using high resolution image in device.

 imageview a;
InputStream ims = getAssets().open("sam.png");//sam.png=520*1400 device=320*480 or 480*800
Drawable d=Drawable.createFromStream(ims, null);
a.setLayoutParams(new        LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
a.setImageDrawable(d);

by using above code image leaves spaces on top and bottom to next contents or If I shrink an image by giving fixed px, its get blur image on its size. Anyway to solve this issue?

arshad kr
  • 357
  • 3
  • 16

2 Answers2

0

Try to create Bitmap instead of Drawable:

Bitmap bmp = BitmapFactory.decodeStream(ims);
a.setImageBitmap(bmp);

Looks like Android doing some tricks with drawables, depending of screen density.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
0

Hope following solution helps. You can make your imageView of fixed size and pass that imageView's width and height to calculateInSampleSize method. Based on image size, it will decide whether to down sample the image or not.

public Bitmap getBitmap(Context context, final String imagePath)
{
    AssetManager assetManager = context.getAssets();
    InputStream inputStream = null;
    Bitmap bitmap = null;
    try
    {
        inputStream = assetManager.open(imagePath);         

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = true;
        options.inJustDecodeBounds = true;

        // First decode with inJustDecodeBounds=true to check dimensions
        bitmap = BitmapFactory.decodeStream(inputStream);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);

        options.inJustDecodeBounds = false;

        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    catch(Exception exception) 
    {
        exception.printStackTrace();
        bitmap = null;
    }

    return bitmap;
}


public int calculateInSampleSize(BitmapFactory.Options options, final int requiredWidth, final int requiredHeight) 
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if(height > requiredHeight || width > requiredWidth) 
    {
        if(width > height) 
        {
            inSampleSize = Math.round((float)height / (float)requiredHeight);
        } 
        else 
        {
            inSampleSize = Math.round((float)width / (float)requiredWidth);
        }
    }

    return inSampleSize;
}
Braj
  • 2,164
  • 2
  • 26
  • 44
  • what does mean final int height = options.outHeight; final int width = options.outWidth; and how to define? – arshad kr Aug 14 '12 at 10:25
  • those are width and height of bitmap used to compare with imageView width and height. – Braj Aug 14 '12 at 10:27
  • There is some mistaake in your code, you define some options but never use them. Look at the official documentation that now exists: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Vince Nov 06 '14 at 01:20
  • @Vince options is used. Have a clear look at the code. – Braj Nov 06 '14 at 06:59
  • I meant to decode the stream. My code looks like using BitMapFactory.decodeStream(inputStream, rect, options). In your code, you read and write the options but never apply them to anything (at least I cannot see where). – Vince Nov 06 '14 at 14:50
  • @Vince I am using `options` to get the `inSampleSize` value to be considered while decoding. Its not compulsory to use `options` while decoding. – Braj Nov 07 '14 at 06:49