error with setPixels

Viewed 19244

i am trying to edit images. but i am getting errors with setPixels.

        picw = pic.getWidth();
        pich = pic.getHeight();
        picsize = picw*pich;        
        int[] pix = new int [picsize];
        pic.getPixels(pix, 0, picw, 0, 0, picw, pich);  
        pic.setPixels(pix,0,pic.getWidth(),0,0,pic.getWidth(),pic.getHeight());

but i am getting illegal state exception with setPixels

Caused by: java.lang.IllegalStateException
  at android.graphics.Bitmap.setPixels(Bitmap.java:878)
  at com.sandyapps.testapp.testapp.onCreate(testapp.java:66)
5 Answers

I had the same problem. Use to fix it:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.my_bitmap, opt );

I was facing this problem and finally fixed after long time.

public static void filterApply(Filter filter){
    Bitmap bitmcopy = PhotoModel.getInstance().getPhotoCopyBitmap();
    //custom scalling is important to apply filter otherwise it will not apply on image
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmcopy, bitmcopy.getWidth()-1, bitmcopy.getHeight()-1, false);
    filter.processFilter(scaledBitmap);
    filterImage.setImageBitmap(scaledBitmap);
}
Related