Reputation: 13
i have to change the color of just one pixel of an selected image from gallery i used a button to change this pixel but always the application has forced to stop when i clicked the button plz help me to solve this problem :( this is my button code
public void btnClick2 (View v){
bmp.setPixel(30,30,0xFF000000 );
}
and this is onactivityresult code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECTED_PICTURE && resultCode == RESULT_OK && null != data)
{
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
bmp = BitmapFactory.decodeFile(picturePath);
iv1.setImageBitmap(bmp);
}
}
Upvotes: 1
Views: 1171
Reputation: 157447
it does force close because BitmapFactory.decodeFile
returns an immutable Bitmap
, while setPixel
works only on mutable bitmaps. You can use Bitmap.copy to get a mutable version of the original bitmap.
Edit:
Bitmap tmpBmp = BitmapFactory.decodeFile(picturePath);
bmp = tmpBmp.copy(Bitmap.Config.ARGB_8888 ,true);
Upvotes: 1