I'm developing an app with many images. Some of the images I moved to expansion files, because of the app limit(50MB). Before I put all my images into different folders like drawable-xhdpi, drawable-mdpi, etc. Now I'm programmatically determining what dpi is the phone and getting images accordingly. Here are the important bits of code:
public String getFolder()
{
String folder = "";
int dpi = res.getDisplayMetrics().densityDpi;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
float smallestWidth = Math.min(widthDp, heightDp);
if (smallestWidth > 720 && dpi == DisplayMetrics.DENSITY_MEDIUM) {
//Device is a 10" tablet
}
else if (dpi == DisplayMetrics.DENSITY_XHIGH||dpi == DisplayMetrics.DENSITY_TV) {
//Device is a 7" tablet
//xhdpi
folder = "drawable-xhdpi/";
}
else if((smallestWidth > 480 && dpi == DisplayMetrics.DENSITY_MEDIUM)|| dpi == DisplayMetrics.DENSITY_HIGH)
{
//hdpi
folder = "drawable-hdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_XXHIGH)
{
//xxhdpi
folder = "drawable-xxhdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_MEDIUM)
{
//mdpi
folder = "drawable-mdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_LOW)
{
//ldpi
folder = "drawable-ldpi/";
}
return folder;
}
public BitmapDrawable getDrawable(Context ctx, String imageName){
String path = folder+imageName;
ZipResourceFile zip;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b = null;
try {
zip = APKExpansionSupport.getAPKExpansionZipFile(ctx, 9, -1);
InputStream is = zip.getInputStream(path);
b = BitmapFactory.decodeStream(is, null, bfo); }
catch (IOException e) {
e.printStackTrace();
}
return new BitmapDrawable(b);
}
So, whenever I need an image I call getDrawable like so:
newItem.setBackgroundDrawable(storage.getDrawable(this,"red_stored.png"));
But the problem is, now when I do it programmatically, the image appears smaller than if I would put it into drawable-xhdpi folder and android authomatically picks it from that folder. What can I do to solve this problem?