无法从 android 工作室中的 Link 加载图像

cannot get to load images from a Link in android studio

我有这段代码,它从 drawables 文件夹中获取图像。我需要从 URL 获取图像。我想我可以使用 Picasso 来做到这一点,但我试过了,但我无法做到。

我的 MainActivity 中有这段代码:

public Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "image_name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

private Drawable d1 = LoadImageFromWebOperations("http://uupload.ir/files/aud7_brickone.jpg");

private List<App> getApps() {
    List<App> apps = new ArrayList<>();
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    apps.add(new App("Google+", d1, 4.6f));
    return apps;
}

这是我的适配器:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    App app = mApps.get(position);
    holder.imageView.setImageResource(app.getDrawable());
    holder.nameTextView.setText(app.getName());
    holder.ratingTextView.setText(String.valueOf(app.getRating()));
}

这里APP.JAVA

public class App {
    private Drawable mDrawable;
    private String mName;
    private float mRating;

    public App (String name, Drawable drawable, float rating){
        mName = name;
        mDrawable = drawable;
        mRating = rating;
    }

    public float getRating (){return mRating;}
    public Drawable getDrawable (){return mDrawable;}
    public String getName (){return mName;}
}

我需要来自 link 的图像,例如:http://uupload.ir/files/aud7_brickone.jpg

我做不到!

你也可以使用滑行, 这是代码

Glide.with(Your context).load(your image url).into(holder.Your imageview);

在您的代码中

Glide.with(context).load(app.getDrawable()).into(holder.imageView);

从 URL 下载图像有多种方法。检查以下功能:

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "any_image_name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

然后只需在 ImageView.

中显示返回的可绘制对象

不要忘记在您的清单中添加互联网权限:

<uses-permission android:name="android.permission.INTERNET" />

您也可以使用其他方法,结合 AsyncTask 或后台线程:

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

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

  protected Bitmap doInBackground(String url) {
      String urldisplay = url;
      Bitmap mIcon = null;
      try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon = BitmapFactory.decodeStream(in);
      } catch (Exception e) {
          Log.e("Error", e.getMessage());
          e.printStackTrace();
      }
      return mIcon;
  }

  protected void onPostExecute(Bitmap result) {
      bmImage.setImageBitmap(result);
  }
}

然后你可以在任何需要的地方调用它:

new DownloadImageTask((ImageView) findViewById(R.id.imageView))
        .execute("http://uupload.ir/files/aud7_brickone.jpg");

希望对您有所帮助:)

您可以使用 GlidePicasso。但我更喜欢你 Picasso 因为 Picasso 响应比 Glide 快。这是代码:

导入 Picasso 库,它就像魅力一样:

Picasso.with(context).load("image url").into(imageview);

如果你需要从网上获取图像,你可以使用 Imageloader,它比 picaso 好得多 get/put JSON 你无法加载或获取图像或文本或任何没有 JSON