2017-02-22 198 views
0

我是新來的android,我在一個應用程序上工作。我想下載一個圖像並將其顯示在列表項中。我爲此使用了許多功能。我的代碼無誤地運行,但圖像不顯示在我的代碼的一部分屏幕上。下載圖像並將其加載到listitem中的imageview中

public loader(Context context, String[] img) { 
     super(context, R.layout.item, img); 

     this.context = context; 
     this.img = img; 

     inflater = LayoutInflater.from(context); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // ImageView imgshow = (ImageView) findViewById(R.id.image1); 

     if (null == convertView) { 
      convertView = inflater.inflate(R.layout.item, parent, false); 

     } 
     ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview); 
     //first method 
     // Glide.with(context) 
     // .load(img[position]) 
     //.into(imageView); 
     //second method 
     DownloadImageTask download=new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview)); 
     download.execute("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg"); 
     //new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageview)) 
     //.execute(img[position]); 
     //third method 
     //Picasso.with(context).load(img[position]).into(imageView); 
     //Log.e("image_url","https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Square_200x200.svg/1024px-Square_200x200.svg.png"); 
     return convertView; 





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


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

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = "http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg"; 

      Bitmap mIcon11 = null; 
      // Toast.makeText(mainpage.this ,urldisplay ,Toast.LENGTH_SHORT).show(); 

      TextView t=(TextView)findViewById(R.id.texttt); 
      // t.setText(urldisplay); 

      try { 


       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

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


     } 
    } 


} 

我把用在我的名單(IMG是一個字符串我保存網址,並顯示它們的陣列,但對於測試我的裝載機類中一個圖像的URL替換)

String[] img = new String[1000]; 
    loader im=new loader(mainpage.this,img); 

    lv.setAdapter(im); 
+0

您應該使用picasso或Glide來緩存和加載圖像。 – Piyush

+0

http://stackoverflow.com/a/9288544/7320259檢查這個 –

+0

只需傳遞你在DownloadImageTask中首次聲明的imageView,如下所示:'DownloadImageTask download = new DownloadImageTask(imageView);' –

回答

0

你無法直接在ImageView中設置圖像。你必須到圖像轉換成位圖,然後設置

public static Bitmap getBitmapFromURL(String src) { 
    try { 
     Log.e("src",src); 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     Log.e("Bitmap","returned"); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.e("Exception",e.getMessage()); 
     return null; 
    } 
} 

然後設置ImageView的:

imageView.setImageBitmap(getBitmapFromURL(url)); 

試試這個,希望它可以幫助你

0

使用piccasso。

要使用piccasso增加其添加

compile 'com.squareup.picasso:picasso:2.5.2' 

到您的應用水平gradle這個。而 使用這種

Picasso.with(context).load("http://previews.123rf.com/images/faysalfarhan/faysalfarhan1402/faysalfarhan140200008/25989999-Back-left-arrow-icon-glossy-purple-button-Stock-Photo.jpg").into(imageview); 

或者你getCount將()方法返回0。 (或添加它)將其更改爲

@Override 
    public int getCount() { 
     return img.length; 
    } 
+0

我使用這個,但沒有圖像顯示 –

+0

我不明白什麼是計數?! –

+0

@ShoroukAdel它是一個在適配器覆蓋的方法 –

0
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
+1

考慮添加一些描述和評論給你的答案。 –

0

使用滑翔

依賴滑翔:編譯 'com.github.bumptech.glide:滑翔:3.7.0'

,並添加代碼

ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 

    Glide.with(this).load("https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg").into(imageView); 
+0

我在listitem中使用了這個但沒有圖像顯示在imageview中。儘管我用來顯示列表中的圖像,但它工作,但在listitem裏面,它沒有 –

+0

通過:[https://futurestud.io/tutorials/glide- listadapter-列表視圖-gridview的] –

相關問題