2017-08-17 77 views
1

我有这段代码,它从drawables文件夹中获取图像。我需要从URL获取图片。我认为我可以用Picasso做到这一点,我试过了,我不能钉它。无法从android工作室中的链接加载图像

我有这样的代码在我的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

我无法实现!

+0

你想从绘制文件夹或远程图像的图像? – Rajesh

+0

遥控!但我需要它转换为“int” –

回答

1

有不同的方法来从URL下载图像。检查以下funcion:

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

希望它有帮助:)

+0

谢谢margabro,我用你的第一个解决方案,现在我有说应用程序“方法的变化第二个参数‘’从‘INT’到‘绘制’。 –

+0

,当一个错误我这样做,我得到另一个错误,在onBindViewHolder代码我Adpater.Java类:holder.imageView.setImageResource(应用程序。getDrawable()); –

+0

catlog中的错误是:错误:(45,58)错误:不兼容的类型:Drawable不能转换为int –

0

您可以使用滑行过, 这里是代码

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

在你的代码

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

您可以使用GlidePicasso。但我更喜欢Picasso,因为Picasso响应速度快于Glide。下面是代码:

导入Picasso库和它的作品般的魅力:

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

u可以使用ImageLoader的其所以比PICASO如果u需要从净u需要得到获取图像更好/把JSONü不能负荷或者得到的图像或文字或任何不JSON

+0

我在这里得到一个错误:holder.imageView.setImageResource(app.getDrawable());它说“错误:(45,58)错误:不兼容的类型:Drawable不能转换为int” –

相关问题