I have one image which was saved into two files: png and jpeg. Then I load them using default colour depth( Bitmap.Config.ARGB_8888
)
Allocation tracker shows that both images consumed 1904016 bytes. Ok, looks fine. But then I added Bitmap.Config.RGB_565
and jpg image consumed 952016 bytes but png image still use 1904015. Why so?
public class MainActivity extends Activity {
private BitmapDrawable png;
private BitmapDrawable jpg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnDecode8888).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
decode(null);
}
});
findViewById(R.id.btnDecode565).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
decode(options);
}
});
}
private void decode(BitmapFactory.Options options) {
png = decodeFile("/mnt/sdcard/img.png", options);
jpg = decodeFile("/mnt/sdcard/img.jpg", options);
}
private BitmapDrawable decodeFile(String path, BitmapFactory.Options options){
BitmapDrawable result = null;
try {
FileInputStream file = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(file, null, options);
result = new BitmapDrawable(getResources(), bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
}