【问题标题】:converting ByteArray File from server to a Image in imageView将 ByteArray 文件从服务器转换为 imageView 中的图像
【发布时间】:2014-04-29 19:25:51
【问题描述】:

我的服务器上有一个 ByteArray 文件,我想读取它并在 ImageView 中显示。

我该怎么做?

【问题讨论】:

  • 将您的 byteArray 转换为位图,然后设置 ImageView 的位图。 Bitmap bm = BitmapFactory.decodeByteArray(myByteArray, 0, byteArray.length);
  • 请不要在主题行中添加“已解决”,而是接受您的/答案以表明您的问题/问题已解决。
  • 我必须等待几个小时才能接受我的回答 :)

标签: android arrays image filestream


【解决方案1】:

将您的 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

【讨论】:

  • 好的,但是如何从服务器下载/读取 ByteArray 呢?
【解决方案2】:

我找到了我的解决方案,代码如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2013-03-05
    相关资源
    最近更新 更多