android - Creating bitmap from canvas java -
solution
thanks @chandrasekhar's suggestions issue was passing in immutable bitmap canvas constructor. solution create copy of when using bitmapfactory.decodefile();
bitmap bmp = bitmapfactory.decodefile(imageurl).copy(bitmap.config.argb_8888, true); so have bitmap using bitmapfactory.decodefile() , works. able create bitmap, need create canvas , things weird.
here's flow of happening. capture image, pass functiona sizes it, , saves out , returns file path. ( using phonegap cordova )
i pass url java , use saved image , manipulate in functionb
code in question:
// url image final jsonobject options = optionsarr.optjsonobject(0); string imageurl = options.optstring("image"); // create image bitmap bitmap bmp = bitmapfactory.decodefile(imageurl); bmp = bitmap.createbitmap(bmp,0,0,655,655); /* works fine until point */ // create image canvas canvas canvas = new canvas(bmp); bitmap 1 = bitmap.createbitmap(bmp); canvas.drawbitmap(one,0,0,null); i receive no errors, hangs. here's kick in pants - if run function functionb first 1 works other doesn't.
i thought maybe needed flush , close first fileoutputstream, didn't seem have effect. i've tried different variable names elements, bitmaps, canvas, , fileoutputstreams.
here example of full function... note: because using phonegap / cordova returning string
public string none(jsonarray optionsarr) { // set file path string filepath = ""; file path = new file(environment.getexternalstoragedirectory()+"/myapp/"); // tmp.jpg store our temporary version of image file newfilepath = new file(path, "tmp_nbb.jpg"); // create folders if needed try{ boolean success = false; if(!path.exists()){ success = path.mkdir(); } if (!success){ log.d("none","folder not created."); } else{ log.d("none","folder created!"); } } catch (exception e){ e.printstacktrace(); } // url image final jsonobject options = optionsarr.optjsonobject(0); string imageurl = options.optstring("image"); // create image bitmap bitmap bmp = bitmapfactory.decodefile(imageurl); bmp = bitmap.createbitmap(bmp,0,0,655,655); // create image canvas canvas canvas = new canvas(bmp); bitmap none = bitmap.createbitmap(bmp); canvas.drawbitmap(none,0,0,null); // save image try { // output stream fileoutputstream out = new fileoutputstream(newfilepath); none.compress(bitmap.compressformat.jpeg, 100, out); // file path uri uri = uri.fromfile(newfilepath); filepath = uri.tostring(); try{ out.flush(); out.close(); // return file path return filepath; } catch (exception e){ e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } return filepath; } like said works first image, when attempt open image again, based on returned filepath chunks out @ create canvas line.
edit: image path using looks this: /mtn/sdcard/myapp/tmp.jpg thoughts?
bitmap 1 = bitmap.createbitmap(bmp); in above code bmp bitmap , creating bitmap object one bmp.
remove line , try changing
canvas.drawbitmap(one,0,0,null); to
canvas.drawbitmap(bmp,0,0,null);
Comments
Post a Comment