Converting an image from drawable to byte array in Android -


since sending image parse.com, have convert byte array. first approach select image gallery , convert byte array follows:

 @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == result_load_image && resultcode == result_ok && null != data) {              mmediauri = data.getdata();             string[] filepathcolumn = { mediastore.images.media.data };              cursor cursor = getcontentresolver().query(mmediauri,                     filepathcolumn, null, null, null);             cursor.movetofirst();              int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             picturepath = cursor.getstring(columnindex);             cursor.close();             // imageview imageview = (imageview) findviewbyid(r.id.imgview);             propertyimage.setimagebitmap(bitmapfactory.decodefile(picturepath));              bitmap bmp = bitmapfactory.decodefile(picturepath);             bytearrayoutputstream stream = new bytearrayoutputstream();             bmp.compress(bitmap.compressformat.png, 100, stream);             bytearray = stream.tobytearray();          } 

the above code works fine , image stored parse. now, when no image selected, app crashes. bcoz, no data sent , parse exception raised.

now, want set default image, in drawable folder go parse, in case -no image selected gallery, parse operations not disturbed null data.

my approach set default image in starting itself:

propertyimage=(imageview)findviewbyid(r.id.imageviewofimage);         propertyimage.setimageresource(r.drawable.blank_image); 

now, how convert default image bytearray, can sent parse?

thanks , regards

check working code:

first need convert drawable image bitmap using method.

 public static bitmap drawabletobitmap(drawable drawable) {     if (drawable instanceof bitmapdrawable) {         return ((bitmapdrawable) drawable).getbitmap();     }      final int width = !drawable.getbounds().isempty() ? drawable             .getbounds().width() : drawable.getintrinsicwidth();      final int height = !drawable.getbounds().isempty() ? drawable             .getbounds().height() : drawable.getintrinsicheight();      final bitmap bitmap = bitmap.createbitmap(width <= 0 ? 1 : width,             height <= 0 ? 1 : height, bitmap.config.argb_8888);      log.v("bitmap width - height :", width + " : " + height);     canvas canvas = new canvas(bitmap);     drawable.setbounds(0, 0, canvas.getwidth(), canvas.getheight());     drawable.draw(canvas);      return bitmap; } 

after getting bitmap object need convert byte array using.

bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.png, 100, stream); byte[] bytearray = stream.tobytearray(); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

gradle error "Cannot convert the provided notation to a File or URI" -