1

Is it possible to load my drawables for hdpi, mdpi and ldpi from given URLs at runtime and use them ?

If not, how can I overcome the denisity problem ?

ohadinho
  • 6,894
  • 16
  • 71
  • 124
  • 1
    What manner of "density problems" are you having? – Ernir Nov 25 '12 at 16:53
  • I just want to get support for multiple screen denisity http://developer.android.com/guide/practices/screens_support.html#support – ohadinho Nov 25 '12 at 16:56
  • That very link contains best practice for this kind of situation - provide drawables for different densities by putting them in a res/ subfolder called drawable-[density], where [density] is ldpi, mdpi, hdpi or xhdpi. – Ernir Nov 25 '12 at 17:02
  • yes.. and I want to get that functionality dynamically – ohadinho Nov 25 '12 at 17:03

3 Answers3

3

Based on screen density you can set the image URL,

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                url = "your_ldpi_url";
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                url = "your_mdpi_url";
                break;
     case DisplayMetrics.DENSITY_HIGH: 
                url = "your_hdpi_url";  
                break;
}

And you can fetch that image and use,

try {
    InputStream is = (InputStream) this.fetch(url);
    image = Drawable.createFromStream(is,"src");
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Dennis Mathew
  • 408
  • 5
  • 14
0

It's impossible. You can't do this after you have built project. Use this:

res/drawable-ldpi/my_icon.png        // bitmap for low density
res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
0

If you really, really want to, you can get the screen density at runtime and download a bitmap depending on the result. However, this is an extraordinarily inefficient way to support multiple screen densities, and should be avoided.

If you need to appropriately resize an image that you have to download (rather than one that will be present on every run), take a look at the BitmapDrawable class.

Community
  • 1
  • 1
Ernir
  • 333
  • 1
  • 8
  • 17