Rakymzhan Zhomartov
Rakymzhan Zhomartov

Reputation: 122

Android scaling copy of image instead of changing source image

I have the following code for getting small sized avatar image:

    Bitmap b= BitmapFactory.decodeFile(selectedImagePath);
    File file = new File(selectedImagePath);
    Bitmap out = Bitmap.createScaledBitmap(b, 128, 128, false);
    FileOutputStream fOut;
    try {
        fOut = new FileOutputStream(file);
        out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        b.recycle();
        out.recycle();
    } 

The problem is that after executing this code, my image in gallery is rescaled too, but I must leave it without any changes. I tried to use copy of Bitmap image like this:

Bitmap bmp2 = b.copy(b.getConfig(), true);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false);

or

Bitmap bmp2 = Bitmap.createBitmap(b);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false

But my original image still changes. How could I obtain new, independent copy of this image?

Upvotes: 0

Views: 36

Answers (1)

Mihaly Nagy
Mihaly Nagy

Reputation: 41

You are loading the image from "selectedImagePath" and then your output file points to the same path, when you call compress() you overwrite your original image.

Try making a new name for the resized image.

fOut = new FileOutputStream(renamedFile);

Upvotes: 1

Related Questions