Change uri in cursor to load images from local dierctory - android

I downloaded example where gallery loaded images like
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns, null, null, orderBy);
When I print MediaStore.Images.Media.EXTERNAL_CONTENT_URI I get:
content://media/external/images/media .
But how can I change to load from directory like:
Environment.getExternalStorageDirectory() + "/test/";
I tried
Uri uri=Uri.parse(Environment.getExternalStorageDirectory() + "/test/");
and
Uri uri=Uri.parse("content:"Environment.getExternalStorageDirectory() + "/test/");
but it doesn't work. Can anybody help me?

Try like this,
File ff = new File("/sdcard/test");
Cursor imagecursor = managedQuery(Uri.fromFile(ff), columns, null, null, orderBy);

Related

Sorting order music player android

ContentResolver contentResolver = context.getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, selection, null, MediaStore.Audio.Media.TITLE + " ASC");
I want to sort audio by title name but running the above code ,i get capital letter audio first and then all the small letter audio.
how do i get absolute sort order,not mixed ordered list?
like from A to z
Try with
Cursor cursor = contentResolver.query(uri, null, selection, null, MediaStore.Audio.Media.TITLE + " COLLATE NOCASE ASC");
See if this works. I have not tried it, Just read somewhere long time back.

Why my cursor is always null?

Following code I use to get images from gallery
final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader( this,sourceUri,null,null,null,MediaStore.Audio.Media.TITLE );
Cursor cursor = cursorLoader.loadInBackground();
here cursor fetches all the images but when I give a specific path as uri like below
String dirUri = "/storage/emulated/myimages";
Cursor cursor = getContentResolver().query( Uri.parse(dirUri), null, null, null, null );
then cursor always gets null value.
How to resolve this ?
Try out as below to get the image from particular folder.
String dirUri =Environment.getExternalStorageDirectory()+ "/myimages";
Cursor cursor = getContentResolver().query(Uri.parse(new File(dirUri).toString()), null, null, null, null );
Use this code instead of your code:
File images = new File(Environment.getExternalStorageDirectory()
+ "/myimages");
Cursor cursor = getContentResolver().query( Uri.parse(images.toString()), null, null, null, null );

Cursor query from specific image folder, not load all image from sdcard

The code below will return all image from the SD card. But, I need to modify it so that it only display images from other folders.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = mContext.getContentResolver();
Cursor mCursor = mContentResolver.query(mImageUri, null, null, null, null);
I have tried this:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/";
Uri uri = Uri.parse(path);
Cursor mCursor = mContentResolver.query(Uri, null, null, null, null);
and am getting an error. Any help would be appreciated.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// field data which u need
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};
Cursor mCursor = mContentResolver.query(mImageUri, columns, MediaStore.Images.Media.DATA + " like ? ",new String[] {"%/YourFolderName/%"}, null);
ArrayList<String> fileNames =null
File path =
File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path,
"Your Folder Name")
if (path.exists()) {
fileNames = path.getList() //you may need to to adjust the Data type of fileNames
}
So, now you have list of images path that you can pass on to recycler view using recycler view Adapter.

how to obtain picture in camera with MediaStore

I try to use MediaStore to get picutures that stored in SDcard:DCIM/Camera.my code is like this:
Cursor track = mContext.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
but I get all pictures that in SDcards,I need get only in DCIM/Camera,help me please.
Try following code.
String SD_CARD_CAM_DIR = Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "Camera";
File file =new File(SD_CARD_CAM_DIR);
Cursor track = mContext.getContentResolver().query(Uri.fromFile(file), null, null,null,null);

sql where clause for managedQuery android method

I am trying to write the parameters to the managedQuery method in android and having trouble with the WHERE clause, here is what i wrote;
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA = filename, proj, null );
"filename" is a string variable holding the path of an image, example /mnt/sdcard/pic05.png
I want it to return a cursor holding a record that is the same record for the pic05.png image and the cursor returned would hold the TITLE column information for this specific picture. How did i mess up the sql WHERE CLAUSE? I thought that my syntax must be wroing on the where clause
Add ' ' around filename. Try this
Cursor imageCursor;
// lots of other junk
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, null );

Categories

Resources