Question

I am using the following code:

private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 75;
        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;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        o2.inScaled = false;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}



public Drawable getProfileImageJSON(String id) {
    String url = "http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2011/05/image_scaling_android.jpg";
    Bitmap b;
    MemoryCache memoryCache = new MemoryCache();

    try {

        Bitmap memCacheBitmap = memoryCache.get(url);

        if (memCacheBitmap != null)
            b = memCacheBitmap;

        else {
            FileCache fileCache = new FileCache(context);
            File f = fileCache.getFile(url);
            Bitmap fileCacheBitmap = decodeFile(f);

            if (fileCacheBitmap != null)
                b = fileCacheBitmap;
            else {
                InputStream is = (InputStream) new URL(url).getContent();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                b = decodeFile(f);
                memoryCache.put(url, b);
            }
        }

        b.setDensity(Bitmap.DENSITY_NONE);


        Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, 75, 75, true));


        // Drawable d = Drawable.createFromStream(is, "src");

        if (d.getIntrinsicHeight() == 0 || d.getIntrinsicHeight() == -1) {
            d = context.getResources().getDrawable(
                    R.drawable.defaultprofile);
            return d;
        }

        else {
            return d;
        }

    }

    catch (Exception e) {
        // Drawable d2 = getResources().getDrawable( R.drawable.icon );


        Drawable d = context.getResources().getDrawable(
                R.drawable.defaultprofile);
        return d;
    }
}

If the URL is invalid, it gets a 75 * 75px default image from drawable folder.

In the case where URL is valid - The code displays a 75 * 75px image in a Sony Xperia phone. However the size is reduced in a Google Nexus phone (looks like a ~ 50 * 50 px image)

So the problem is with the screen density, I guess. I need the image to be 75 * 75 px on all screens. How do I solve this problem?

Solution - Use DisplayMetrics.Convert dp to pixels. Take the the density of the default image and divide the pixel value with the dpi value of the image( in this case it was in the hdpi folder, so 1.5)

public float convertDpToPixel(float dp, Activity context)

{ Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f)/1.5f; return px; }

Was it helpful?

Solution

Use below method :-

 Drawable d = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(b, ((int) convertDpToPixel(75)),  ((int) convertDpToPixel(75)), true));

function convertDpToPixel

public float convertDpToPixel(float dp, Activity context)
{
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

Your static value change according to ppi of every device so use above code.

more info :-

getting the screen density programmatically in android?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top