java - Android bitmap to ByteArray and vice versa returns null? -


i trying allocate direct bytebuffer java, fill bitmap , create bitmap buffer. result receive null.

    bitmapfactory.options options = new bitmapfactory.options();     options.inpreferredconfig = bitmap.config.argb_8888;     mcurrentbitmap = bitmapfactory.decodefile(hardcodedpath, options);      // 4 - bytes count per pixel     bytescount = mcurrentbitmap.getwidth() * mcurrentbitmap.getheight() * 4;      pixels = bytebuffer.allocatedirect((int) bytescount);     mcurrentbitmap.copypixelstobuffer(this.pixels);      byte[] bitmapdata = new byte[pixels.remaining()];     pixels.get(bitmapdata);      bitmapfactory.options opt = new bitmapfactory.options();     opt.inpreferredconfig = bitmap.config.argb_8888;      bitmap newbitmap = bitmapfactory.decodebytearray(bitmapdata, 0, (int) bytescount, opt); 

can me understand why newbitmap null?

decodebytearray() expects encoded bitmap data (png, jpeg, etc.), not simple rgb(a) byte array. want can use bitmap.setpixels(). first, create bitmap of right size/configuration (bitmap.create(width, height, bitmap.config.argb_8888) instance) call setpixels(bitmapdata, 0, width, 0, 0, width, height) on it.

since have bytebuffer can more calling bitmap.copypixelsfrombuffer(). setpixels(), create bitmap of right size first.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -