1

I am writing a function, which takes a bitmap, edits it and saves it. But the problem is that the saved image is not having the same dimensions. Can anyone help me fix it? I want the output image to be of same length and width as that of the original image.

This is my code -

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();

}

Here the original size is having dimensions of 288x192 but the output image has dimensions of 605x403. Where am i wrong?

Also i have tried changing the quality, but it doesn't works.

Kaal_Hari
  • 15
  • 7

1 Answers1

-1

When placing the image resources in drawable directory, the decoded image size depends upon the screen size of the running device.

Keep your drawable inside the folder drawable-nodpi rather than other drawable directories so that the image size remains preserved.

Also rather than using BitmapDrawable, you can directly decode a bitmap from resource using BitmapFactory.decodeResource()

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59