I am creating an update for my application, in it I have a folder with saved images that I would like to display in the GridView. I am already using the Ion library . The creator of the library (Koush Dutta) already has a sample that does what I want and displays all the images from the SD card in the GridView. .
What I want to do is display only images from a specific folder on my SD card (the so-called comics) in the GridView. I use the code directly from the example above, only changing some names.
My problem is that I canβt only get the images from the SD card in the GridView, currently it is getting all the images from the SD card. I want only from the folder / sdcard / Comics /
The code
public class SavedComics extends Activity { private MyAdapter mAdapter; // Adapter to populate and imageview from an url contained in the array adapter private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context) { super(context, 0); } @Override public View getView(int position, View convertView, ViewGroup parent) { // see if we need to load more to get 40, otherwise populate the adapter if (position > getCount() - 4) loadMore(); if (convertView == null) convertView = getLayoutInflater().inflate(R.layout.image, null); // find the image view final ImageView iv = (ImageView) convertView.findViewById(R.id.comic); // select the image view Ion.with(iv) .centerCrop() .error(R.drawable.error) .load(getItem(position)); return convertView; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery); int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi * 2; GridView view = (GridView) findViewById(R.id.results); view.setNumColumns(cols); mAdapter = new MyAdapter(this); view.setAdapter(mAdapter); loadMore(); } Cursor mediaCursor; public void loadMore() { if (mediaCursor == null) { mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), null, null, null, null); } int loaded = 0; while (mediaCursor.moveToNext() && loaded < 10) { // get the media type. ion can show images for both regular images AND video. int mediaType = mediaCursor.getInt(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE)); if (mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE && mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) { continue; } loaded++; String uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)); File file = new File(uri); // turn this into a file uri if necessary/possible if (file.exists()) mAdapter.add(file.toURI().toString()); else mAdapter.add(uri); } } }
I tried several things, one of which was similar to this answer :
getContentResolver().query( uri1, Selection, android.provider.MediaStore.Audio.Media.DATA + " like ? ", new String[] {str}, null);
source share