-1

I am trying to show all my pictures in a listview. I have an sqlite db where i save the image's path and now i wanted to know how can i show the image. I already know how to convert the path in a bitmap but i really have no ideia how to put it in the 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);

Right now i have a listview with the photos title and when i click in the title the image appears in an imageview but i wanted to show the images. Does anyone know how can i do that or does anyone know a better way than a listview to show the images and how can i do that??

  • 1
    you can create a custom listview with imageviews.. if you need text view also then you can add that too. And populate it using custom adapter. – OnePunchMan May 09 '14 at 13:13
  • I just needed the photos. how can i do a listview with imageviews?? – user2257792 May 09 '14 at 13:16
  • create an array list of Bitmap, and store all imges in it OR you can save all the paths in an array list of string, and convert to bitmap later. For listview, If you are going to put just only the image, there might be empty sapaces left. If you fine with that, then make an xml layout with just imageView. And inflate in the listview. There is grid layout also, it comes handy with images – OnePunchMan May 09 '14 at 13:26

1 Answers1

0

image View in an 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>

main activity 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>

Main Activity class: ImageListView, I am showing the images that are in the **res->drawable-mdpi folder

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

Custom 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;
}
}
OnePunchMan
  • 720
  • 15
  • 33