Android,在URL上创建一个等于ImageView图像的图像

Ski*_*zit 38 android

我想知道如何制作一个位于特定URL的图像等于ImageView的图像?

Pri*_*han 143

下载图像并将其设置为imageview的内容

try {
  ImageView i = (ImageView)findViewById(R.id.image);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  i.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)


Rea*_*oid 7

  // Url = "url of image"
  Drawable drawable = LoadImageFromWebOperations(Url);
  mImageofTheMonth.setImageDrawable(drawable);

private Drawable LoadImageFromWebOperations(String url) {

    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }

}
Run Code Online (Sandbox Code Playgroud)


yme*_*ene 5

由于我没有足够的积分来添加评论,我会发帖子......

记得在AsyncTask doInBackground中放置@ primpap的答案,以防止UI线程冻结.