2010-12-27 4 views
0

Я хочу создать активность, которая будет загружать изображения из Интернета с помощью URL-адресов, и я хочу загрузить эти изображения в виде списка, и я хочу, чтобы какой-то конкретный текст и свойства этого изображения перед этим изображением изображение ниже Click to see Image. Есть ли способ сохранить это изображение временно в памяти телефона. Так как я могу это сделать, пожалуйста, дайте мне идеальное решение, чтобы я мог завершить свое приложение. Как загрузить изображения из Интернета.Android List view

+0

Repost из http://stackoverflow.com/questions/4536890/layout-styles-in-android/4536919#4536919 – sahhhm

+2

Покажите нам, что у вас есть .... я не пишу все приложение для вы. – st0le

ответ

0

Давайте попробуем, как этот

URL img_value = новый URL (IMG [положение]);

   Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 

       holder.image.setImageBitmap(mIcon1); 

здесь holder.image - это виджет изображения, хранящийся в вашем xml для просмотра изображений.

Он напрямую загружает изображение с данного URL-адреса. или попробуйте сделать это ниже, создав класс с именем DrawableManager ...

новый DrawableManager(). FetchDrawableOnThread (VAL3 [position], holder.icon);

package com.fsp.demo; 
import java.io.IOException; 
import java.io.InputStream; 
import java.lang.ref.SoftReference; 
import java.net.MalformedURLException; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.graphics.drawable.Drawable; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.widget.ImageView; 

public class DrawableManager { 
    @SuppressWarnings("unchecked") 
    private final Map drawableMap; 

    public DrawableManager() { 
     drawableMap = new HashMap<String, SoftReference<Drawable>>(); 
    } 

    @SuppressWarnings("unchecked") 
    public Drawable fetchDrawable(String urlString) { 
     SoftReference<Drawable> drawableRef = (SoftReference<Drawable>) drawableMap 
       .get(urlString); 
     if (drawableRef != null) { 
      Drawable drawable = drawableRef.get(); 
      if (drawable != null) 
       return drawable; 
      // Reference has expired so remove the key from drawableMap 
      drawableMap.remove(urlString); 
     } 

     if (Constants.LOGGING) 
      Log.d(this.getClass().getSimpleName(), "image url:" + urlString); 
     try { 
      InputStream is = fetch(urlString); 
      Drawable drawable = Drawable.createFromStream(is, "src"); 
      drawableRef = new SoftReference<Drawable>(drawable); 
      drawableMap.put(urlString, drawableRef); 
      if (Constants.LOGGING) 
       Log.d(this.getClass().getSimpleName(), 
         "got a thumbnail drawable: " + drawable.getBounds() 
           + ", " + drawable.getIntrinsicHeight() + "," 
           + drawable.getIntrinsicWidth() + ", " 
           + drawable.getMinimumHeight() + "," 
           + drawable.getMinimumWidth()); 
      return drawableRef.get(); 
     } catch (MalformedURLException e) { 
      if (Constants.LOGGING) 
       Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", 
         e); 
      return null; 
     } catch (IOException e) { 
      if (Constants.LOGGING) 
       Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", 
         e); 
      return null; 
     } 
    } 

    @SuppressWarnings("unchecked") 
    public void fetchDrawableOnThread(final String urlString, 
      final ImageView imageView) { 
//  Log.v("Drawable_url",urlString); 
     SoftReference<Drawable> drawableRef = (SoftReference<Drawable>) drawableMap 
       .get(urlString); 
     if (drawableRef != null) { 
      Drawable drawable = drawableRef.get(); 
      if (drawable != null) { 
       imageView.setImageDrawable(drawableRef.get()); 
       return; 
      } 
      // Reference has expired so remove the key from drawableMap 
      drawableMap.remove(urlString); 
     } 

     final Handler handler = new Handler() { 
      public void handleMessage(Message message) { 
       imageView.setImageDrawable((Drawable) message.obj); 
      } 
     }; 

     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       // TODO : set imageView to a "pending" image 
       Drawable drawable = fetchDrawable(urlString); 
       Message message = handler.obtainMessage(1, drawable); 
       handler.sendMessage(message); 
      } 
     }; 
     thread.start(); 
    } 

    private InputStream fetch(String urlString) throws MalformedURLException, 
      IOException { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(urlString); 
     HttpResponse response = httpClient.execute(request); 
     return response.getEntity().getContent(); 
    } 

}