Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

android - Displaying images from specific folder in sd card in grid view

I had developed an application in which I have to display images from specific folder in sd card. My code is working fine but it is showing all images that are sd card. I had given selection args as my folder's name as it has been metioned the following post: Displaying images from a specific folder on the SDCard using a gridview

But, it didn't worked. Please, suggest me how to do this: Below I am posting my code.

public class Album1Activity extends Activity {
static Cursor imageCursor;
static int columnIndex;
GridView gridView;
Intent in;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    String[] imgColumnID = {MediaStore.Images.Thumbnails._ID};
    Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
    imageCursor = managedQuery(uri, imgColumnID, null, null, 
                    MediaStore.Images.Thumbnails.IMAGE_ID);
    columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(getApplicationContext()));
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            String[] dataLocation = {MediaStore.Images.Media.DATA};
            //String []dataLocation = {Environment.getExternalStorageDirectory().getAbsolutePath()};
            imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                        dataLocation, null, null, null);
            columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            imageCursor.moveToPosition(position);
            String imgPath = imageCursor.getString(columnIndex);
            in = new Intent(getApplicationContext(), FullScreen.class);
            in.putExtra("id", imgPath);
            startActivity(in);
        }//onItemClickListener
    });
}//onCreate

public static class ImageAdapter extends BaseAdapter{
    private Context context;
    //Album1Activity act = new Album1Activity();
    public ImageAdapter(Context context){
        this.context = context;
    }//ImageAdapter
    public int getCount() {
        // TODO Auto-generated method stub
        return imageCursor.getCount();
    }//getCount

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv;
        if(convertView == null){
            iv = new ImageView(context);
            imageCursor.moveToPosition(position);
            int imageID = imageCursor.getInt(columnIndex);
            iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                            "" +imageID));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setPadding(5, 5, 5, 5);
            iv.setLayoutParams(new GridView.LayoutParams(100, 100));
        }//if
        else{
            iv = (ImageView)convertView;
        }//else
        return iv;
    }//getView

}//ImageAdapter

}//Album1Activity

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry for Answering late... Below I am posting the final solution

public class Album3 extends Activity {
File [] mediaFiles;
File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
ImageButton btncam;
String name = null;
ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";

public void onCreate(Bundle savedInstanceState) 
{
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    super.onCreate(savedInstanceState);
    if((imageDir.exists()))
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.grid);
        mediaFiles = imageDir.listFiles();
        //Log.d("Length of images",""+mediaFiles.length);
        for(File file : mediaFiles)
        {
            bmpArray.add(convertToBitmap(file));
            fileName.add(readFileName(file));
            //Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
            //Log.d(TAG, "call to convertToBitmap");
        }//for

        adapter = new ImageAdapter(this, bmpArray, fileName);
        gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(adapter);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

        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);
        }
    });
    }//if
    else
    {
        setContentView(R.layout.no_media);
    }
}//onCreate

public static Bitmap convertToBitmap(File file) 
{
    URL url = null;
    try 
    {
        url = file.toURL();
    } catch (MalformedURLException e1) 
    {
        //Log.d(TAG, e1.toString());
    }//catch

    Bitmap bmp = null;
    try
    {
       bmp = BitmapFactory.decodeStream(url.openStream());
       //bmp.recycle();
    }catch(Exception e)
    {
        //Log.d(TAG, "Exception: "+e.toString());
    }//catch
    return bmp;
}//convertToBitmap

public String readFileName(File file){
    String name = file.getName();
    return name;
}//readFileName
}//class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...