androidandroid-sdcardandroid-gallerybaseadaptergalleryview

displaying images from sd card folder in gallery view(android)


I am developing android application.In my application I want to display images from sd card folder in gallery view. I am using BaseAdapter. I tried to display images from drawable folder in resources folder in my project and it's working fine. I tried following code:

public class ImageAdapter extends BaseAdapter {

private Context context;  
public static Integer[] imageIDs={  
        R.drawable.sp1,R.drawable.sp2,
        R.drawable.sp3,R.drawable.sp4,
        R.drawable.sp5,R.drawable.sp6,
        R.drawable.sp7,R.drawable.sp8,
        R.drawable.sp9
};

public ImageAdapter(Context context){  
    this.context=context;  
}  
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imageIDs.length;
}

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

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

@Override
public View getView(int position, View 
        convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView image=new ImageView(context);  
    image.setImageResource(imageIDs[position]);  
    image.setAdjustViewBounds(true);  
    image.setLayoutParams(new Gallery.LayoutParams(120,120));  
    image.setScaleType(ImageView.ScaleType.FIT_CENTER);
    return image;  
}

Now I want to display images from sd card folder and display them in gallery format. for that I tried to get all images in array but I don't know how to get them in array format. I can access my images in sd folder in following manner.

File path = new File(Environment.getExternalStorageDirectory()+"/download/sp1.jpg");

How to get them all in array format and display them in gallery view.Or is there any other alternate way to display them instead of array?

Need help.... Thank you....


Solution

  • See below code

    if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
    
            // It have to be matched with the directory in SDCard
            boolean exist = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "download").exists();
            if (exist) {
                File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "download");
    
                File[] files = f.listFiles();
                File file;
                for (int i = 0; i < files.length; i++) {
                    file = files[i];
                    Bitmap bitmap = decodeFile(file);
                    return bitmap;      
                }
            }
        }
    

    Can you do this way

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return files.length;
    }
    
    
    
    @Override
    public View getView(int position, View 
            convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView image=new ImageView(context);  
    
        //image.setImageResource(imageIDs[position]);  
        File file = files[position]
        Bitmap bitmap = decodeFile(file);
        image.setImageBitmap(bitmap);
    
        image.setAdjustViewBounds(true);  
        image.setLayoutParams(new Gallery.LayoutParams(120,120));  
        image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return image;  
    }