How to query MediaStore.Images.Media.BUCKET_DISPLAY_NAME? - android

I can use the following code to query Media._ID, Media.BUCKET_DISPLAY_NAME and DATE_TAKEN of every photo,
but I only hope to get MediaStore.Images.Media.BUCKET_DISPLAY_NAME, how can I do ? Thanks!
// which image properties are we querying
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// Make the query.
Cursor cur = managedQuery(images,
projection, // Which columns to return
"", // Which rows to return (all rows)
null, // Selection arguments (none)
"" // Ordering
);
Log.i("ListingImages"," query count="+cur.getCount());
if (cur.moveToFirst()) {
String bucket;
String date;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);
do {
// Get the field values
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
// Do something with the values.
Log.i("ListingImages", " bucket=" + bucket
+ " date_taken=" + date);
} while (cur.moveToNext());
}

String[] projection = new String[]{
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
};
instead of
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
???

Related

How query images/videos in custom folders using ContentResolver in Android?

I have a custom folder in the Pictures directory, like thisPictures/MyFolder. It has images in MyFolder. Here is how to query the images using ContentResolver on MyFolder folder only.
I tried this
Cursor mediaCursor = context.getContentResolver().query(
MediaStore.Files.getContentUri("external"),
null,
MediaStore.MediaColumns.RELATIVE_PATH + " like ? ",
new String[]{"%MyFolder%"},
null);
But it contains other files also. Or is any alternate to content resolver?
You can use the below function to get the images from the folder.
private void getImageFolderList() {
String[] projection = new String[] { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN };
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor cur = getContentResolver().query(images, projection, // Which
// columns
// to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
orderBy + " DESC" // Ordering
);
ArrayList<String> imagePath;
if (cur.moveToFirst()) {
String bucket, date;
int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
do {
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
if (!allFolder.contains(bucket)) {
allFolder.add(bucket);
}
imagePath = listImageByFolder.get(bucket);
if (imagePath == null) {
imagePath = new ArrayList<String>();
}
imagePath.add(cur.getString(cur
.getColumnIndex(MediaStore.Images.Media.DATA)));
listImageByFolder.put(bucket, imagePath);
} while (cur.moveToNext());
}
}

Get media files and display in the list

I'm a beginner in android. According to following the document 'Access media files from shared storage'. I want to display name all audio in the list, but it only shows one audio. Could someone help me to solve this? Thanks. Here my code.`
ArrayList<audio> arrayList = new ArrayList<audio>();
String[] projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.SIZE
};
String selection = MediaStore.Audio.Media.DURATION +
" >= ?";
String[] selectionArgs = new String[]{
String.valueOf(TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES))};
String sortOrder = MediaStore.Audio.Media.DISPLAY_NAME + " ASC";
try (Cursor cursor = v.getContext().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
)) {
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int durationColumn =
cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
while (cursor.moveToNext()) {
long id = cursor.getLong(idColumn);
String name = cursor.getString(nameColumn);
int duration = cursor.getInt(durationColumn);
int size = cursor.getInt(sizeColumn);
Uri contentUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
// Stores column values and the contentUri in a local object
// that represents the media file.
arrayList.add(new audio(name));
}
}
`

How to retrieve a single contact

how can I retrieve a single contact and some associated data (e.g. emails, phonenumbers, addresses...) by its id/lookupkey?
This is the code I use to add contacts (actually its from the internet and is working for me).
// Asking the Contact provider to create a new contact
try {
result = this.context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this.context, "Exception: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
Uri myContactUri = result[0].uri;
int lastSlash = myContactUri.toString().lastIndexOf("/");
int length = myContactUri.toString().length();
int contactID = Integer.parseInt((String) myContactUri.toString()
.subSequence(lastSlash + 1, length));
return contactID;
Now I want to fetch this new contact. How do I do it? All I came up with is this:
ContentResolver content = context.getContentResolver();
String[] projection = { Data.LOOKUP_KEY, Data.MIMETYPE,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.ADDRESS };
// Defines the selection clause
String selection = Data.LOOKUP_KEY + " = ?";
// Defines the sort order
String sortOrder = Data.LOOKUP_KEY;
String[] args = {"2400"};
Cursor cursor = content.query(Data.CONTENT_URI, projection, selection,
args, sortOrder);
When I remove the selection I get all contacts+all their data. So I looked up the key 2400 in my case and wanted to fetch this contact by its lookupkey. Well, does not work. cursor.getCount() return 0.
Any ideas?
My solution now is to use to following:
String[] projection = { Data.MIMETYPE,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.ADDRESS };
// Defines the selection clause
String selection = ContactsContract.Data.RAW_CONTACT_ID + "=?";
// Defines the sort order
String sortOrder = Data.LOOKUP_KEY;
String[] args = { id+"" };
Cursor cursor = content.query(Data.CONTENT_URI, projection, selection,
args, sortOrder);
Sort order doesnt matter, but I use the RAW_CONTACT_ID, which works well!

Not able to fetch the correct Birthday and Anniversary data from Contact

I am able to fetch other information (Display name,organisation,phone no and email_id) of a contact, but not able to fetch birthday and anniversary of that contact.
Here is the code i am using for birthday. It does fetch the data, but gives me wrong data, i.e repeats the same data for all the contacts.
private String getBDate(String id) {
String bday = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, sortOrder);
while (cur.moveToNext()) {
bday = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Birthday", bday);
}
cur.close();
return bday;
}
Same is the case with anniversary, here is the code for it. In some case anniversary is not added but it still shows the data from other contact.
private String getAnnv(String id) {
String annv = null;
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE };
String where = ContactsContract.Data.MIMETYPE + "= ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + "="
+ ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY;
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
// String sortOrder = null;
Cursor cur = cr.query(uri, projection, where, selectionArgs, null);
while (cur.moveToNext()) {
annv = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
Log.v("Anniversary", annv);
}
cur.close();
return annv;
}
you are not using String id perameter in where condition so please check again.
E,g private String getAnnv(String id) function has input for ID but that seems to be not used withing function so please put that ID in condition check and this should work.
e.g
ContactsContract.CommonDataKinds.Event.CONTACT_ID + "= " + ID
AND ContactsContract.Data.MIMETYPE + "= ? AND "

How to get name of bitmap from MediaStore.Images

I am getting bitmaps from MediStore like
MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(),id,MediaStore.Images.Thumbnails.MICRO_KIND, null);
My question is how to get name of every picture ( bitmap ) ?
Try below code to get the name of the pics and it's folder from the external storage
String[] projection = new String[] { MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DISPLAY_NAME };
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = managedQuery(images, projection, // Which columns to return
"", // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Media.BUCKET_DISPLAY_NAME // Ordering
);
Log.i("ListingImages", " query count=" + cur.getCount() + "Columns ="
+ cur.getColumnCount() + "" + cur.getColumnName(0) + ""
+ cur.getColumnName(1) + "" + cur.getColumnName(2));
if (cur.moveToFirst()) {
int bucketColumn = cur
.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dpColumn = cur
.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
do {
Log.i("Folder Name",cur.getString(bucketColumn));
Log.i("Pic Name ",cur.getString(dpColumn));
} while (cur.moveToNext());
}
else
{
Log.i("Ooops","No Media Found");
}

Categories

Resources