使用路径在listview中显示照片

时间:2014-05-09 13:02:30

标签: android android-listview android-gallery

我想在列表视图中显示我的所有照片。我有一个sqlite数据库,我保存图像的路径,现在我想知道如何显示图像。我已经知道如何在位图中转换路径,但我真的没有想法如何将它放在listview中

File imgFile = new  File(caminhoNovo);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inSampleSize = 4;
    //This is the path already converted to image
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options); 
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);

现在我有一个带有照片标题的列表视图,当我点击标题时,图像出现在图像视图中,但我想显示图像。有谁知道我怎么能这样做或有没有人知道比列表视图更好的方式来显示图像,我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

image以xml格式查看

<?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" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

主要活动xml

<?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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="List of Images"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true" >
</ListView>

</RelativeLayout>

主要活动类:ImageListView,我正在显示** res-&gt; drawable-mdpi文件夹中的图像

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class ImageListView extends Activity {

    private ImageAdapter adapter;
    private ListView imageList;

    private ArrayList<Integer> imagePaths= new ArrayList<Integer>(); // Edit your code here..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_listview);

        // Edit your code here..
        imagePaths.add(R.drawable.image1);
        imagePaths.add(R.drawable.image2);
        imagePaths.add(R.drawable.image3);

        imageList= (ListView) findViewById(R.id.listView1);
        adapter= new ImageAdapter(getBaseContext(), imagePaths);
        imageList.setAdapter(adapter);      
    }
}

自定义适配器:ImageAdapter

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    static class RowItemHolder{
        ImageView imageView;
    }
    private Context context;
    private ArrayList<Integer> imagePaths= new ArrayList<Integer>();

    public ImageAdapter(Context baseContext, ArrayList<Integer> imagePaths) {
    // TODO Auto-generated constructor stub
        this.context= baseContext;
        this.imagePaths= imagePaths;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
        return imagePaths.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
            return null;
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
            return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view;
    view= convertView;
    RowItemHolder holder = null;
    if(view== null){
            LayoutInflater in =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = in.inflate(R.layout.image_view, parent, false);
            holder= new RowItemHolder();
            holder.imageView=(ImageView) view.findViewById(R.id.imageView1);
        view.setTag(holder);
    } else{
            holder = (RowItemHolder) view.getTag();
    }
    //Edit the code here according to you needs.. 
    //like creating option and converting to Bitmap, 
    //or you can do this job in the main activity.
    holder.imageView.setImageResource(imagePaths.get(position));
    return view;
}
}