无法从android studio

时间:2017-08-17 07:53:59

标签: android drawable picasso

我有这个代码,它从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;}
}

我需要来自以下链接的图片:http://uupload.ir/files/aud7_brickone.jpg

我无法实现!

4 个答案:

答案 0 :(得分:1)

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

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

代码

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

答案 1 :(得分:1)

从网址下载图片有多种方法。检查下面的功能:

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中显示已退回的drawable。

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

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

希望有所帮助:)

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

你可以使用Imageloader比picaso更好,如果你需要从网络获取图像你需要获取/放置JSON你无法加载或获取图像或文本或没有JSON的任何东西