Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error with setPixels

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)
like image 802
sandeep Avatar asked Jul 20 '11 15:07

sandeep


3 Answers

I think your Bitmap is not mutable (see setPixel()'s documentation).

If so, create a mutable copy of this Bitmap (using Bitmap.copy(Bitmap.Config config, boolean isMutable) as an example) and work on this one.

like image 95
Shlublu Avatar answered Oct 14 '22 16:10

Shlublu


It's simple, just use the following command to change it to a mutable Bitmap:

myBitmap = myBitmap.copy( Bitmap.Config.ARGB_8888 , true); 

Now the Bitmap myBitmap is replaced by the same Bitmap but this time is mutable

You can also choose another way of storing Pixels (ARGB_8888 etc..): https://developer.android.com/reference/android/graphics/Bitmap.Config.html

like image 25
YOGO Avatar answered Oct 14 '22 17:10

YOGO


Most probably your pic is immutable. By default, any bitmap created from drawable would be immutable.

If you need to modify an existing bitmap, you should do following:

// Create a bitmap of the same size
Bitmap newBmp = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Config.ARGB);
// Create a canvas  for new bitmap
Canvas c = new Canvas(newBmp); 

// Draw your old bitmap on it. 
c.drawBitmap(pic, 0, 0, new Paint());
like image 34
xandy Avatar answered Oct 14 '22 17:10

xandy