【发布时间】:2014-04-22 21:32:55
【问题描述】:
我有一张图片保存在两个文件中:png 和 jpeg。然后我使用默认颜色深度加载它们(Bitmap.Config.ARGB_8888)
分配跟踪器显示两个图像都消耗了 1904016 字节。好的,看起来不错。但是后来我添加了Bitmap.Config.RGB_565,而jpg图像消耗了952016字节,而png图像仍然使用1904015。为什么会这样?
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;
}
}
【问题讨论】:
-
你确定你都像 565 一样解码吗?
-
它可以重复吗?如果您要重复体验,内存分配的数量是否完全相同?
-
@blackbelt 是的,我添加了完整代码。
-
@Tonio 是的,我重复了几次。分配仍然相同。
标签: android memory memory-management bitmap