Java 使用URL图像的Android自定义ListView

Java 使用URL图像的Android自定义ListView,java,android,xml,listview,Java,Android,Xml,Listview,我一直在尝试让我的ListView填充来自适配器已知的网站URL的图像。活动会使每个图像膨胀,但我的模拟器甚至不会启动,只会显示黑色。我在整个代码中搜索都没有结果,尝试了很多东西,但不幸的是,我现在被卡住了。 第一个类是我的适配器类,它为每个索引提供一个视图 第二个类是我的活动类,它加载视图。第三个类是helper类 还有两个布局XML文件和一个XML清单 GalleryAdapter.java import java.io.IOException; import java.io.InputSt

我一直在尝试让我的ListView填充来自适配器已知的网站URL的图像。活动会使每个图像膨胀,但我的模拟器甚至不会启动,只会显示黑色。我在整个代码中搜索都没有结果,尝试了很多东西,但不幸的是,我现在被卡住了。 第一个类是我的适配器类,它为每个索引提供一个视图 第二个类是我的活动类,它加载视图。第三个类是helper类

还有两个布局XML文件和一个XML清单

GalleryAdapter.java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.example.imageshow.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class GalleryAdapter extends ArrayAdapter<JSONObject>{
    DrawableManager dM = new DrawableManager();
    Server s = new Server();
    Context ctx;
    LayoutInflater myInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public GalleryAdapter(Context context, int resource) {
        super(context, resource);
        ctx = context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView==null){
            convertView = myInflater.inflate(R.layout.gallery_item, parent, true);
        }
        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText(s.galleryObjects.get(position).getName());
        TextView eventDate = (TextView) convertView.findViewById(R.id.eventDate);
        eventDate.setText(s.galleryObjects.get(position).getEventDate());
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        String imageUrl = s.galleryObjects.get(position).getImageURL();

        //which one works??
        //1
        image.setImageDrawable(dM.fetchDrawable(imageUrl));
        //2
        try {
              image = (ImageView) convertView.findViewById(R.id.image);
              Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
              image.setImageBitmap(bitmap); 
            } catch (MalformedURLException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
        //3
        //new DownloadImageTask(image).execute(s.galleryObjects.get(position).getImageURL());
        return convertView;
    }



    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;
        }
    }

    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

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

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            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);
        }
    }

}
import com.example.imageshow.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class GalleryActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        GalleryAdapter p = new GalleryAdapter(this,0);
        setContentView(R.layout.galleries);
        ListView l = (ListView) findViewById(R.id.galleryList);
        ImageView moreButton = (ImageView) findViewById(R.id.moreButton);
        ImageView cameraButton = (ImageView) findViewById(R.id.cameraButton);
        moreButton.setOnClickListener(new MoreButtonListener());
        cameraButton.setOnClickListener(new CameraButtonListener());
        l.setAdapter(p);
        setVisible(true);
    }

    public class MoreButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    }

    public class CameraButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    }
}
import java.io.IOException;
import java.io.InputStream;
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 {
    private final Map<String, Drawable> drawableMap;

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

    public Drawable fetchDrawable(String urlString) {
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        }

        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");


            if (drawable != null) {
                drawableMap.put(urlString, drawable);
                Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
                        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                        + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
            } else {
              Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
            }

            return drawable;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        } catch (IOException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
        if (drawableMap.containsKey(urlString)) {
            imageView.setImageDrawable(drawableMap.get(urlString));
        }

        final Handler handler = new Handler() {
            @Override
            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();
    }
}
DrawableManager.Java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.example.imageshow.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class GalleryAdapter extends ArrayAdapter<JSONObject>{
    DrawableManager dM = new DrawableManager();
    Server s = new Server();
    Context ctx;
    LayoutInflater myInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public GalleryAdapter(Context context, int resource) {
        super(context, resource);
        ctx = context;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView==null){
            convertView = myInflater.inflate(R.layout.gallery_item, parent, true);
        }
        TextView name = (TextView) convertView.findViewById(R.id.name);
        name.setText(s.galleryObjects.get(position).getName());
        TextView eventDate = (TextView) convertView.findViewById(R.id.eventDate);
        eventDate.setText(s.galleryObjects.get(position).getEventDate());
        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        String imageUrl = s.galleryObjects.get(position).getImageURL();

        //which one works??
        //1
        image.setImageDrawable(dM.fetchDrawable(imageUrl));
        //2
        try {
              image = (ImageView) convertView.findViewById(R.id.image);
              Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
              image.setImageBitmap(bitmap); 
            } catch (MalformedURLException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
        //3
        //new DownloadImageTask(image).execute(s.galleryObjects.get(position).getImageURL());
        return convertView;
    }



    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;
        }
    }

    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

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

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            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);
        }
    }

}
import com.example.imageshow.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class GalleryActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        GalleryAdapter p = new GalleryAdapter(this,0);
        setContentView(R.layout.galleries);
        ListView l = (ListView) findViewById(R.id.galleryList);
        ImageView moreButton = (ImageView) findViewById(R.id.moreButton);
        ImageView cameraButton = (ImageView) findViewById(R.id.cameraButton);
        moreButton.setOnClickListener(new MoreButtonListener());
        cameraButton.setOnClickListener(new CameraButtonListener());
        l.setAdapter(p);
        setVisible(true);
    }

    public class MoreButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    }

    public class CameraButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

    }
}
import java.io.IOException;
import java.io.InputStream;
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 {
    private final Map<String, Drawable> drawableMap;

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

    public Drawable fetchDrawable(String urlString) {
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        }

        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");


            if (drawable != null) {
                drawableMap.put(urlString, drawable);
                Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
                        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
                        + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
            } else {
              Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
            }

            return drawable;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        } catch (IOException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null;
        }
    }

    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
        if (drawableMap.containsKey(urlString)) {
            imageView.setImageDrawable(drawableMap.get(urlString));
        }

        final Handler handler = new Handler() {
            @Override
            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();
    }
}
import java.io.IOException;
导入java.io.InputStream;
导入java.net.MalformedURLException;
导入java.util.HashMap;
导入java.util.Map;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.graphics.drawable.drawable;
导入android.os.Handler;
导入android.os.Message;
导入android.util.Log;
导入android.widget.ImageView;
公共类DrawableManager{
私有最终地图drawableMap;
公共提款经理(){
drawableMap=新HashMap();
}
公共可提取可提取(字符串urlString){
if(drawableMap.containsKey(urlString)){
返回drawableMap.get(urlString);
}
试一试{
InputStream is=fetch(urlString);
Drawable Drawable=Drawable.createFromStream(是“src”);
如果(可绘制!=null){
drawableMap.put(urlString,drawable);
Log.d(this.getClass().getSimpleName(),“获得了可绘制的缩略图:“+drawable.getBounds()+”,”
+drawable.getIntrinsicHeight()+”,“+drawable.getIntrinsicWidth()+”,”
+可绘制的.getMinimumHeight()+”,“+drawable.getMinimumWidth());
}否则{
Log.w(this.getClass().getSimpleName(),“无法获取缩略图”);
}
回拉;
}捕获(格式错误){
Log.e(this.getClass().getSimpleName(),“fetchDrawable失败”,e);
返回null;
}捕获(IOE异常){
Log.e(this.getClass().getSimpleName(),“fetchDrawable失败”,e);
返回null;
}
}
public void fetchDrawableOnThread(最终字符串urlString,最终图像视图ImageView){
if(drawableMap.containsKey(urlString)){
setImageDrawable(drawableMap.get(urlString));
}
最终处理程序=新处理程序(){
@凌驾
公共无效handleMessage(消息消息){
setImageDrawable((Drawable)message.obj);
}
};
线程线程=新线程(){
@凌驾
公开募捐{
//TODO:将imageView设置为“挂起”图像
Drawable Drawable=fetchDrawable(urlString);
Message Message=handler.getainmessage(1,可绘制);
handler.sendMessage(message);
}
};
thread.start();
}
私有InputStream获取(字符串urlString)引发错误的InputStream异常,IOException{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(urlString);
HttpResponse response=httpClient.execute(请求);
返回response.getEntity().getContent();
}
}
布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fbfbfb" >
            <ImageView
                android:id = "@+id/cameraButton"
                android:layout_height="wrap_content"
                android:layout_width = "wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:clickable = "true"
                android:adjustViewBounds="true"
                android:padding="10dp"
                android:maxHeight="400dp"
                android:src="@drawable/ic_launcher"
                android:background="#b9b9b9">
            </ImageView>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:clickable="false"
                android:maxHeight="400dp"
                android:text="SPORTSPHOTOS.com"
                android:textColor="#123123" >
            </TextView>
            <ImageView
                android:id = "@+id/moreButton"
                android:layout_height="wrap_content"
                android:layout_width = "wrap_content"
                android:layout_alignParentRight="true"
                android:adjustViewBounds="true"
                android:clickable="true"
                android:padding="10dp"
                android:maxHeight="60dp"
                android:layout_centerVertical="true"
                android:src="@drawable/more"
                >
            </ImageView>
        </RelativeLayout>
        <ScrollView
            android:layout_width = "fill_parent"
            android:layout_height="fill_parent">

            <ListView
                android:id="@+id/galleryList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </ListView>

        </ScrollView>


    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:padding="20dp"
        android:orientation="vertical"
    >

    <TextView
        android:id="@+id/name"
        android:text="Great Wall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">
    </TextView>

    <TextView
        android:id="@+id/eventDate"
        android:text="08-15-2014"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">

    </TextView>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="200dp"
            android:padding="10dp">  
        </ImageView>

    </LinearLayout>

    </LinearLayout>

</RelativeLayout>

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fbfbfb" >
            <ImageView
                android:id = "@+id/cameraButton"
                android:layout_height="wrap_content"
                android:layout_width = "wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:clickable = "true"
                android:adjustViewBounds="true"
                android:padding="10dp"
                android:maxHeight="400dp"
                android:src="@drawable/ic_launcher"
                android:background="#b9b9b9">
            </ImageView>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:clickable="false"
                android:maxHeight="400dp"
                android:text="SPORTSPHOTOS.com"
                android:textColor="#123123" >
            </TextView>
            <ImageView
                android:id = "@+id/moreButton"
                android:layout_height="wrap_content"
                android:layout_width = "wrap_content"
                android:layout_alignParentRight="true"
                android:adjustViewBounds="true"
                android:clickable="true"
                android:padding="10dp"
                android:maxHeight="60dp"
                android:layout_centerVertical="true"
                android:src="@drawable/more"
                >
            </ImageView>
        </RelativeLayout>
        <ScrollView
            android:layout_width = "fill_parent"
            android:layout_height="fill_parent">

            <ListView
                android:id="@+id/galleryList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </ListView>

        </ScrollView>


    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:padding="20dp"
        android:orientation="vertical"
    >

    <TextView
        android:id="@+id/name"
        android:text="Great Wall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">
    </TextView>

    <TextView
        android:id="@+id/eventDate"
        android:text="08-15-2014"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">

    </TextView>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="200dp"
            android:padding="10dp">  
        </ImageView>

    </LinearLayout>

    </LinearLayout>

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imageshow"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SECOND" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GalleryActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我认为这些解决方案都行不通。确保通过http加载图像在后台(不是解决方案2)。在listview中在后台加载图像的主要问题是视图循环()。重用视图时,不应将正在加载的图像设置为初始图像视图。当视图被回收时,应在imageView中加载另一个图像。因此,请跟踪和imageView与url之间的关系。(例如,在将图像设置为imageView之前,将url放入imageView.setTag(url)),检查imageView.getTag()是否与刚才加载的url匹配

然而,为了让它更容易一点,我总是使用AndroidQuery。从网络异步加载映像只需要两行代码:

//load an image to an ImageView from network, cache image to file and memory
AQuery aq = new AQuery(convertView);
aq.id(R.id.image).image("http://www.vikispot.com/z/images/vikispot/android-w.png");

有关更多选项,请参阅。(如缓存等)

在实现自己的
图像加载器时,请尝试使用缓存存储位图,以便它不会总是下载图像。不鼓励在适配器的
getView()
方法中硬下载映像


尝试使用
图像加载库
等。这些库将为您处理图像缓存和磁盘缓存。

您在logcat上是否遇到任何错误?是的,但我不知道它们的任何含义。不过,在第一个类中,我确实将getView()中的true更改为false。发布您的logcat错误以及问题为什么不尝试一些图像加载程序库,如或谢谢,我将使用建议解决方案重试,并发布更新。