如何将二进制数据转换为图像?

在我的Android应用程序中。 我从代码中获取了jpeg图像的二进制代码,如下所示。

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

此字符串s打印图像的二进制值。 我的问题是如何将这种二进制格式转换为jpeg图像?

我不太确定你想要什么。

  • 如果要直接从流创建Bitmap -instance,可以使用BitmapFactory并在之后的ImageView -instance中显示该Bitmap

     Bitmap image = BitmapFactory.decodeStream(stream); imageView.setImageBitmap(image); 
  • 如果要将带有基数2的字符串表示forms转换回二进制数组,也可以使用BigInteger

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

希望这可以帮助

编辑:写入内部存储器

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

写入外部存储器

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

我以前用过这段代码:

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