2017-08-17 85 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」 –

相關問題