简体   繁体   中英

How to set setImageResource, I have URIs of the gallery images of my phone

I have all the URIs of my phone images from Gallery as:

for(String item: items) {
    Log.d("Images", item);
}

its output are:

07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150425_131643.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_213547.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_213646.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_221519.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_221530.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_221544.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_221628.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222055.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222533_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222541_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222552_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222637_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150426_222643_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150429_205140_HDR.jpg
07-04 17:30:22.633  11140-11140/com.example.android.HomeWork2 D/Images﹕ /storage/emulated/0/DCIM/Camera/IMG_20150429_205148_HDR.jpg

I want to populate these in gridview. We can set static image present in Drawable as:

imageView.setImageResource(mThumbIds[position]);

where:

private Integer[] mThumbIds = {
     R.drawable.ic_launcher,R.drawable.ic_launcher
};

My gridview is:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

I am wondering how I can set images dynamically, as I donn't know how many images are there in the gallery and their URIs until runtime.

You will need to use an Adapter that will populate the grids for you. Wait till you have the information about the images, then populate the adapter appropriately. A good tutorial showing how to use gridviews and populate them with images+text can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM