I have an app where i can pick images from the sdcard. Once images have been picked, say from the photos folder, you can view the selected images in a ListView.
The selected images are passed to the listview using an Adapter which takes an array of paths to each image.
It all works fine, but how do make the image fit to the width of the listview row?
thanks in advance.
public class QueuedImagesActivity extends Activity {
private static final String TAG = QueuedImagesActivity.class.getSimpleName();
private ImageAdapter adapter;
private ListView imageList;
private ApplicationObj appObj;
private Intent[] uniquePhotoChunks;
private String path;
private ArrayList<String> imagePaths = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_listview);
appObj = (ApplicationObj) getApplication();
// Get the queued chunks
try {
boolean includeChunksCurrentlyBeingProcessed = true;
boolean returnUniqueUris = true;
uniquePhotoChunks = appObj.getQueuedChunks(includeChunksCurrentlyBeingProcessed, returnUniqueUris);
Log.d(TAG, "There are " + uniquePhotoChunks.length + " photo paths sent back from getQueuedChunks");
//get the URI out from the Intent with getDataString() and store in Array that the adapter will use
for(int i = 0; i < uniquePhotoChunks.length; i++){
path = uniquePhotoChunks[i].getDataString();
imagePaths.add(path);
Log.d(TAG, "path in QueuedImagesActivity = " + path);
}
//pass the array to the adapter
imageList = (ListView) findViewById(R.id.listView1);
adapter = new ImageAdapter(getBaseContext(), imagePaths);
imageList.setAdapter(adapter);
}
catch (Exception e) {
Log.e(TAG, "There was a problem showing the queued images", e);
Toast.makeText(this, "There was a problem showing the queued images", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}//end of onCreate
}
.
<?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>
.
public class ImageAdapter extends BaseAdapter {
private static final String TAG = ImageAdapter.class.getSimpleName();
static class RowItemHolder{
ImageView imageView;
}
private Context context;
private ArrayList<String> imagePaths= new ArrayList<String>();
public ImageAdapter(Context baseContext, ArrayList<String> imagePaths) {
this.context= baseContext;
this.imagePaths= imagePaths;
}
@Override
public int getCount() {
return imagePaths.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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();
}
Log.e(TAG, "imagePaths.get(position) = " + imagePaths.get(position));
holder.imageView.setImageURI(Uri.parse(imagePaths.get(position)));
return view;
}
}
[edit1]
<?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="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>