Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong size from Drawable.getIntrinsicWidth()

Tags:

android

image

I download image with this code:

ImageGetter imageGetter = new ImageGetter() {
    @Override
    public Drawable getDrawable(String source) {
        Drawable drawable = null;
        try {
            URL url = new URL(source);
            String path = Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.my.pkg/"+url.getFile();
            File f=new File(path);
            if(!f.exists()) {
                URLConnection connection = url.openConnection();
                InputStream is = connection.getInputStream();

                f=new File(f.getParent());
                f.mkdirs();                 

                FileOutputStream os = new FileOutputStream(path);
                byte[] buffer = new byte[4096];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.close();
                is.close();
            }
            drawable = Drawable.createFromPath(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        if(drawable != null) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        }
        return drawable;
    }
};

This image has size 20x20. But drawable.getIntrinsicWidth() and drawable.getIntrinsicHeight() return 27. And image looks larger. How I can fix it?

like image 379
BArtWell Avatar asked Nov 18 '25 07:11

BArtWell


2 Answers

BitmapDrawable must scale bitmap to compensate for different screen densities.

If you need it to draw pixel-for-pixel, try setting Drawable's source density & target density to same value. To do this, you need slightly different objects to work with.

Instead of

drawable = Drawable.createFromPath(path);

use

Bitmap bmp = BitmapFactory.decodeFile(path);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
bmp.setDensity(dm.densityDpi);
drawable = new BitmapDrawable(bmp, context.getResources());

If you don't have context (which you should), you can use application context, see e.g. Using Application context everywhere?

Since bitmap's density is set to resources' density, which is actual device's screen's density, it should draw without scaling.

like image 189
user1410657 Avatar answered Nov 20 '25 19:11

user1410657


I tried the code form the answer and did not work. So instead i used the code below and it worked fine.

     DisplayMetrics dm = context.getResources().getDisplayMetrics();

     Options options=new Options();
     options.inDensity=dm.densityDpi;
     options.inScreenDensity=dm.densityDpi;
     options.inTargetDensity=dm.densityDpi;      

     Bitmap bmp = BitmapFactory.decodeFile(path,options);
     drawable = new BitmapDrawable(bmp, context.getResources());
like image 28
Alan Avatar answered Nov 20 '25 19:11

Alan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!