如何保存与原始尺寸相同的位图?

How to save a bitmap with same dimensions that of original?

我正在编写一个函数,它获取位图,对其进行编辑并保存。 但问题是保存的图像没有相同的尺寸。 谁能帮我修一下?我希望输出图像与原始图像具有相同的长度和宽度。

这是我的代码 -

public void bitTest(View v) throws IOException {
    Drawable t=getResources().getDrawable(R.drawable.slider_touch,getTheme());//the image to be processed
    Bitmap bit=((BitmapDrawable) t).getBitmap();//its bitmap
    Bitmap finalImage=Bitmap.createBitmap(bit.getWidth(),bit.getHeight(),bit.getConfig());//creating another bitmap of same dimensions

    Context context=getApplicationContext();
    FileOutputStream fos = context.openFileOutput("test.png", Context.MODE_PRIVATE);//saving
    finalImage.compress(Bitmap.CompressFormat.PNG,100, fos);
    fos.close();

}

此处原始尺寸为 288x192,但输出图像的尺寸为 605x403。 我哪里错了?

我也试过改变画质,但没用。

将图片资源放在drawable目录下时,解码后的图片大小取决于运行设备的屏幕尺寸。

将您的可绘制对象保存在文件夹 drawable-nodpi 中而不是其他可绘制对象目录中,以便图像大小保持不变。

此外,除了使用 BitmapDrawable,您还可以使用 BitmapFactory.decodeResource()

直接从资源中解码位图