2

I had developed an application where I want to display images inside grid view from specific folder of sd card. The application is working but only 1st image from folder is appearing in every grid, while I want all images should display. I am not getting where I went wrong. Below, I am posting my code:

Album Activity:

public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
GridView gridView;
ImageAdapter adapter;
Intent in;
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    prepareList();
    adapter = new ImageAdapter(this, mediaFiles);
    gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            in = new Intent(getApplicationContext(), FullScreen.class);
            in.putExtra("id", position);
            startActivity(in);
        }
    });
}//onCreate

public static Bitmap prepareList() {
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    Bitmap bmp = null;
    for(File imagePath:mediaFiles){
        try{
            bmp = BitmapFactory.decodeStream(imagePath.toURL().openStream());
        }catch(Exception e){
            Log.d(TAG, "Exception: "+e.toString());
        }//catch
    }//for
    Log.d(TAG, "prepareList() called");
    return bmp;
}//prepareList

}//class

Image Adapter:

public class ImageAdapter extends BaseAdapter{
Activity act;
File[] mFiles;
public static final String TAG = "ImageAdapter";
public ImageAdapter(Activity act, File[] mFiles){
    super();
    this.act = act;
    this.mFiles = mFiles;
}//ImageAdapter

public int getCount() {
    return mFiles.length;
}//getCount

public Object getItem(int postion) {
    return mFiles[postion];
}//getItem

public long getItemId(int position) {
    return 0;
}//getItemId

public static class ViewHolder{
    ImageView iv;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder view;
    LayoutInflater li = act.getLayoutInflater();
    if(convertView == null){
        view = new ViewHolder();
        convertView = li.inflate(R.layout.gridview_row, null);
        view.iv = (ImageView)convertView.findViewById(R.id.imageView1);
        convertView.setTag(view);
    }//if
    else{
        view = (ViewHolder)convertView.getTag();
    }//else
    Bitmap bmp = Album3Activity.prepareList();
    view.iv.setImageBitmap(bmp);
    Log.d(TAG, "getView called");
    return convertView;
}//getView

}//ImageAdapter

4

2 回答 2

2

Note prepareList method, it is not returning bitmap according to the position or index of the imageview, so it would return same image all the time, change it to accept an index parameter, and return bitmap accordingly, as:

public static Bitmap prepareList(int index) {
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    File imagePath=imageDir[index];
    Bitmap bmp = null;
        try{
            bmp = BitmapFactory.decodeStream(imagePath.toURL().openStream());
        }catch(Exception e){
            Log.d(TAG, "Exception: "+e.toString());
        }//catch
   Log.d(TAG, "prepareList() called");
    return bmp;
}//prepareList
于 2012-05-12T04:28:01.473 回答
0

In getView everytime your are calling album3Activity.prepareList() so it will return single image for all the grid. Try with the passing the particular imagepath in mFiles[position] each time and get the bitmap or get all the bitmap at once and store it in arrayList and pass the arraylist to adapter and use it in getView.

Try this..it may help you..

于 2012-05-12T04:27:10.120 回答