2014-04-29 59 views
0

我的服务器上有一个ByteArray文件,我想 在ImageView中读取并显示,我该怎么做?将ByteArray文件从服务器转换为imageView中的图像

+0

您的字节组转换为位图,然后设置'ImageView'的位图。 'Bitmap bm = BitmapFactory.decodeByteArray(myByteArray,0,byteArray.length);' – john

+0

而不是将“解决”添加到主题行,请接受您的答案以表明您的问题已经解决。 –

+0

我必须等待几个小时才能接受我的答案:) – Christer

回答

0

将您的byteArray转换为位图,然后设置ImageView的位图。

Bitmap bm = BitmapFactory.decodeByteArray(myByteArray, 0, byteArray.length);//myByteArray is the byte array you want to convert. 
ImageView myImage = (ImageView)findViewById(R.id.custom_image);//get a reference to your ImageView 
myImage.setImageBitmap(bm)//set the ImageView bitmap 
+0

好的,但是如何从服务器下载/读取ByteArray? – Christer

0

,我发现我的解决方案,这里是代码:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // show The Image 
    new DownloadImageTask((ImageView) findViewById(R.id.imageView1)) 
    .execute("http://myURL"); 

} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 
相关问题