how to convert binary data to image?

28,068

Solution 1

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();
    

Solution 2

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);
Share:
28,068
Santhosh_pulliman
Author by

Santhosh_pulliman

Working as an Android application developer at http://www.officialgates.com

Updated on January 07, 2020

Comments

  • Santhosh_pulliman
    Santhosh_pulliman over 4 years

    In my android application. I got binary code from a jpeg image from the code as follows.

    byte[] val = stream.toByteArray();
              BigInteger bi = new BigInteger(val);
        String s =  bi.toString(2);
    

    This string s prints the binary value of the image. My question is how to convert this binary format into a jpeg image??

  • Santhosh_pulliman
    Santhosh_pulliman over 12 years
    I print the binary values as string. I want to convert the binary value to jpeg image.
  • Andreas Klöber
    Andreas Klöber over 12 years
    If you want the opposite conversion as in your example code, look at my answer (second section).
  • Santhosh_pulliman
    Santhosh_pulliman over 12 years
    @Jana Just see Andreas Klober's answer. He understood my question and answered what i need. Actually i wanted to convert the binary format to a jpeg image. Thank you for your response :)
  • Santhosh_pulliman
    Santhosh_pulliman over 12 years
    Klober You only understood my question correctly. This works great. Thank you so much for your response
  • Santhosh_pulliman
    Santhosh_pulliman over 12 years
    @Jana Just see Andreas Klober's answer. He understood my question and answered what i need. Actually i wanted to convert the binary format to a jpeg image. Thank you for your response :)