Unable to round the image corners second time
I am trying to add rounded corners to images in my android app. The code
which I am using for adding rounded corners is:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
final SeekBar cornerRadius = (SeekBar) findViewById(R.id.seekBar1);
final TextView tip = (TextView) findViewById(R.id.tip);
bm = getRoundedCornerBitmap(BitmapFactory.decodeFile(picturePath),
cornerRadius.getProgress());
imageView.setImageBitmap(bm);
The above corner radius is got from the value of seekbar.
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
The code for saving the pic to sd card which I am invoking on click of
save button is:
OutputStream fOut = null;
File file = new File(picturePath);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(MainActivity.this,
"Pic save successfuly. Path : " + picturePath,
Toast.LENGTH_SHORT).show();
The problem I face is that once an image has been edited to have rounded
corners. The next time, I open the same image and try saving with
different corner radius value doesn't save the image. I do get the above
Toast message second time though but the image remains unchanged.
No comments:
Post a Comment