2

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??

1
  • just write the byte array to file Commented Jan 6, 2012 at 12:06

4 Answers 4

4

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();
    
Sign up to request clarification or add additional context in comments.

1 Comment

Klober You only understood my question correctly. This works great. Thank you so much for your response
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);

4 Comments

I print the binary values as string. I want to convert the binary value to jpeg image.
If you want the opposite conversion as in your example code, look at my answer (second section).
@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 :)
@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 :)
0
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

                  }


    }

Comments

0

I've used this code in the past:

InputStream is = (InputStream)imageContent;
d = Drawable.createFromStream(is, "src");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.