how to convert binary data to image?

心不动则不痛 提交于 2019-12-02 01:30:13

I'm not really sure what you want.

  • If you want to create a Bitmap-instance directly from the stream you can use BitmapFactory and display that Bitmap in an ImageView-instance afterwards:

    Bitmap image = BitmapFactory.decodeStream(stream);
    imageView.setImageBitmap(image);
    
  • If you want to convert your string representation with radix 2 back to a binary array you can use BigInteger too:

    BigInteger bigInt = new BigInteger(s, 2);
    byte[] binaryData = bigInt.toByteArray();
    
Bitmap bmp=BitmapFactory.decodeByteArray(val, 0, val.length);
ImageView img = new ImageView(this);
img.setImageBitmap(bmp);

Hope this Helps

Edit: To write in Internal Memory

FileOutputStream fout;
fout = openFileOutput("temp.jpg",Context.MODE_WORLD_WRITEABLE);    
b1.compress(CompressFormat.JPEG, 100, fout);

To write in External Memory

FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/temp.JPEG");
 bm.compress(Bitmap.CompressFormat.JPEG,90, fout);
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
savefile(bMap,"test.jpg");

private void savefile(Bitmap bt,String file)
    {
        try {
            ContextWrapper context=this;
            FileOutputStream stream =context.openFileOutput(file, 2);
            BufferedOutputStream  objectOut = null;
              //    FileOutputStream stream =(FileOutputStream) getAssets().open("temp.txt");
                  try {
                    objectOut = new BufferedOutputStream(stream);
                     bt.compress(Bitmap.CompressFormat.PNG, 100, objectOut);
                     objectOut.flush(); 
                     objectOut.close();
                  }
                  catch (Exception e) {
                   // TODO Auto-generated catch block

                      }

             } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block

                  }


    }

I've used this code in the past:

InputStream is = (InputStream)imageContent;
d = Drawable.createFromStream(is, "src");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!