android Picture.createFromStream does not reload bitmaps -


on android drawing android.graphics.picture save picture file. later reload picture memory , draw canvas. noticed bitmaps never drawing. , after debugging managed narrow down problem picture.writetostream , picture.createfromstream. seems bitmaps drawn picture don't reloaded properly. below sample code wrote show problem. in sample canvas not hardware accelerated.

so questions follows:

  1. am doing wrong?
  2. is android bug? filed bug report https://code.google.com/p/android/issues/detail?id=54896 because think is.
  3. any known workaround?

    @override protected void ondraw(canvas canvas) {     try     {         picture picture = new picture();          // create bitmap         bitmap bitmap = bitmap.createbitmap( 100, 100, config.argb_8888);         canvas bitmapcanvas = new canvas(bitmap);         bitmapcanvas.drawargb(255, 0, 255, 0);          // draw bitmap picture's canvas.         canvas picturecanvas = picture.beginrecording(canvas.getwidth(), canvas.getheight());          rectf dstrect = new rectf(0, 0, 200, 200);         picturecanvas.drawbitmap(bitmap, null, dstrect, null);          picture.endrecording();          // save picture file.         file file = file.createtempfile("cache", ".pic");         fileoutputstream os = new fileoutputstream(file);         picture.writetostream(os);         os.close();          // read picture in         fileinputstream in = new fileinputstream(file);         picture cachedpicture = picture.createfromstream(in);          // draw cached picture view's canvas. won't draw bitmap!         canvas.drawpicture(cachedpicture);          // uncomment following line see drawing picture without reloading          // disk works fine.         //canvas.drawpicture(picture);     }     catch (exception e)     {     } } 

i did find answer question after looking @ native android code backs bitmap. android can save types of bitmaps picture. because skbitmap class supports types of inputs result in bitmap can saved picture. in can can workaround problem providing magical inputs. use bitmap saved disk , call bitmapfactory.decodefiledescriptor create it.

private bitmap createreusablebitmap(bitmap inbitmap) {     bitmap reuseablebitmap = null;      if (inbitmap== null)         return null;      try     {         // caller responsible deleting file.         file tmpbitmapfile = file.createtempfile("bitmap", ".png");          setbitmappath(tmpbitmapfile.getabsolutepath());          fileoutputstream out = new fileoutputstream(tmpbitmapfile);         boolean compressed = inbitmap.compress(compressformat.png, 100, out);         out.close();          if (compressed)         {             // have create purgeable bitmap b/c kind works right when drawing              // picture. after digging through android source found decodefiledescriptor create 1 need.                       // see https://github.com/android/platform_frameworks_base/blob/master/core/jni/android/graphics/bitmapfactory.cpp             // in short have give options inpurgeable=true ininputshareable=true , call decodefiledescriptor              bitmapfactory.options options = new bitmapfactory.options();             options.inpreferredconfig = bitmap.config.argb_8888;             options.ininputshareable = true;             options.inpurgeable = true;             options.insamplesize = 1;             options.inscaled = false;             options.inmutable = false;             options.intempstorage = draftrenderer.tempstorage;               fileinputstream instream = new fileinputstream(tmpbitmapfile);             filedescriptor fd = instream.getfd();             reuseablebitmap = bitmapfactory.decodefiledescriptor(fd, null, options);             instream.close();         }     } catch (exception e) {     }     return reuseablebitmap; } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -