Get images thumbnail file paths - android

I'm trying to get the thumnail paths, not the bitmaps objects.
When I query for these, some of the thumbnail paths are null for some reason.
(I have 1028 thumbnail images in my device, the cursor length is indeed 1028, but still returning nulls) I know there are 1028 thumbnail images because I checked.
Here's my code:
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
Log.d(Global.TAG, "BEFORE");
columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
Log.d(Global.TAG, "AFTER1");
for(int i =0;i<cursor.getCount();i++){
cursor.moveToPosition(i);
Log.d("MyTag","BBABA" + i +" : " + getThumbnailPathForLocalFile(cursor.getLong(columnIndex)));
}
cursor.close();
My getThumbnailPathForLocalFile:
String getThumbnailPathForLocalFile(long fileId)
{
// Log.d(Global., msg)
Cursor thumbCursor = null;
try
{
thumbCursor = this.getContentResolver().
query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI
, null
, MediaStore.Images.Thumbnails.IMAGE_ID + " = " + fileId+ " AND "
+ MediaStore.Images.Thumbnails.KIND + " = "
+ MediaStore.Images.Thumbnails.MINI_KIND , null, null);
if(thumbCursor.moveToFirst())
{
// the path is stored in the DATA column
int dataIndex = thumbCursor.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA );
String thumbnailPath = thumbCursor.getString(dataIndex);
return thumbnailPath;
}
}
finally
{
if(thumbCursor != null)
{
thumbCursor.close();
}
}
return null;
}
Here's my logcat:
http://pastebin.com/UZLZF9Pg
After checking, I see that the ids I send are just like the index of the for loop.
I'm not even sure that my code is supposed to work, so any other code will be great.

You should query for MediaStore.Images.Thumbnails.DATA. To modify your example, it would look like this.
String[] projection = {MediaStore.Images.Thumbnails.DATA};
// Create the cursor pointing to the SDCard
Cursor cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
// Get the column index of the Thumbnails Image ID
Log.d(TAG, "BEFORE");
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
Log.d(TAG, "AFTER1");
for(int i =0;i<cursor.getCount();i++){
cursor.moveToPosition(i);
Log.d("MyTag","BBABA" + i +" : " + cursor.getString(columnIndex));
}
cursor.close();
Reference: How to get imagepath from thumbnail path of a image?

Related

Android, media scan doesn't generate thumbnails?

I have image files which don't have thumbnails. (imported from my external carmera using NFC)
I want to create thumbnails for my image picker view. (has to be fast)
At this moment, I'm not even sure "MEDIA SCAN" means "generating thumbnail" or what.
I tried to scan file using mMediaScannerConnection.scanFile(mPath, null);
onScanCompleted gets called and I try to get thumbnail using the following two version of functions.
I get null thumbnailPath for both functions, I don't get why..
private String getThumbnailPath(long imageId) {
ContentResolver cr = this.mContext.getContentResolver();
Cursor c = MediaStore.Images.Thumbnails.queryMiniThumbnail(cr, imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
try {
if (c.moveToNext()) {
int dataId = c.getColumnIndex( Images.Thumbnails.DATA);
String strThumPath = c.getString(dataId);
Log.i("ScanTest", "strThumPath = " + strThumPath );
return strThumPath;
}
} finally {
if (c != null) c.close();
}
return null;
}
private String getThumbnailPath2( long imageId ){
// http://wikicloud.blogspot.kr/2010/10/scanfile.html
ContentResolver cr = this.mContext.getContentResolver();
Cursor c = cr.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Thumbnails.IMAGE_ID + "= ?" , new String[]{String.valueOf(imageId)}, null);
try {
if (c.moveToNext()) {
int dataId = c.getColumnIndex( Images.Thumbnails.DATA);
String strThumPath = c.getString(dataId);
Log.i("ScanTest", "strThumPath = " + strThumPath );
return strThumPath;
}
} finally {
if (c != null) c.close();
}
return null;
}
-- edit --
Here's how I try to get thumbnails.
first create a mapping from image-id to thumbnail-path.
protected Map getThumbnailPathFromDB() {
Map result = new HashMap();
Cursor thumbCursor = null;
thumbCursor = getContentResolver().query(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
null,
null,
null, null);
if (thumbCursor.moveToFirst()) {
do {
String path = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.MediaColumns.DATA));
long imageId = thumbCursor.getLong(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID));
result.put(new Long(imageId), path);
} while(thumbCursor.moveToNext());
}
thumbCursor.close();
return result;
next I iterate all images and try to find thumbnails from the mapping I created above
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(
Images.Media.EXTERNAL_CONTENT_URI, null,
null, null, Images.ImageColumns.DATE_MODIFIED + " DESC");
//thumbnailPathList is the mapping I created above, imageId is the id of image obtained from the cursor
String thumbnailPath = thumbnailPathList.get(new Long(imageId));
// thumbnailPath are occasionally null here!,
I tested my code on about 10 devices.
They all have images which I can't find thumbnails for.

Contact photo not updating for a select few

I was recently playing around with some code that goes through my contacts and creates an identicon for any contact that does not have a photo set. For the most part this ended up working quite well but for some reason I have a handful of contacts that will not update. Log output says it is creating the photo. The update() returns 1 indicating 1 row was updated and stepping through the code for a contact that never seems to show the new photo looks good.
The fact that only a select few are not updating is what is really bugging me and I'm guessing there must be something I am doing wrong or missing here.
private void processContacts() {
Cursor cursor = getContacts();
Log.d(TAG, "Processing " + cursor.getCount() + " contacts");
while(cursor.moveToNext()) {
final long contactId = cursor.getLong(0);
final String name = cursor.getString(1);
if (!TextUtils.isEmpty(name)) {
final Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contactId);
if(ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
contactUri, true) == null) {
Log.d(TAG, String.format("Creating identicon for %s", name));
generateIdenticon(contactId, name);
} else {
Log.i(TAG, String.format("%s already has a contact photo", name));
}
}
}
cursor.close();
}
private Cursor getContacts() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, null, null, sortOrder);
}
private void generateIdenticon(long contactId, String name) {
if (!TextUtils.isEmpty(name)) {
updateNotification(getString(R.string.identicons_creation_service_running_title),
String.format(getString(R.string.identicons_creation_service_contact_summary),
name));
final byte[] hash = Identicon.generateHash(name);
final byte[] identicon = Identicon.generateIdenticonByteArray(hash);
if (identicon == null) {
Log.e(TAG, "generateIdenticon() - identicon for " + name + " is null!");
} else {
if (!setContactPhoto(getContentResolver(), identicon, contactId)) {
Log.e(TAG, "Unable to save identicon for " + name);
}
}
}
}
private boolean setContactPhoto(ContentResolver resolver, byte[] bytes, long personId) {
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " +
String.valueOf(personId) + " AND " + ContactsContract.Data.MIMETYPE + "=='" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = resolver.query(
ContactsContract.Data.CONTENT_URI,
null,
where,
null,
null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if(cursor.moveToFirst()){
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.Data.IS_PRIMARY, 1);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
final int rowsUpdated = resolver.update(ContactsContract.Data.CONTENT_URI,
values, ContactsContract.Data._ID + "=" + photoRow, null);
return rowsUpdated >= 1;
} else {
final Uri uri = resolver.insert(ContactsContract.Data.CONTENT_URI, values);
return uri != null && !TextUtils.isEmpty(uri.toString());
}
}
All this is being done inside a background service and all my contacts are synced via google. One last thing to note is that these select contacts always return null when I call ContactsContract.Contacts.openContactPhotoInputStream() to see if a photo is available (even after I've attempted to update the photo).
Any help or insight about what may be going on is greatly appreciated.

context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);

Hi I am working in Android Contact search module.I am running below Query.
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
from this query I am getting Result Multiple times.Is there any thing which I am doing wrong.I want DISTINCT Result Set.
please help me.
I think you mean you got duplicate record for some contacts. So you must add condition for your query
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Try this code will help you
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
Each record should contain a portion of the data for a contact (eg each phone number or address is a separate row) each row has a mimetype associated with it that is used to determine the data stored in each column. So for an address, the "data1" column holds the street data and data4 might hold the state.

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");
}

How can i get the album art from MediaStore?

How can I get the album art from MediaStore? I try something like:
String[] projectionImages = new String[]{MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM_KEY};
Cursor c = contentResolver.query(MediaStore.Audio.Albums.INTERNAL_CONTENT_URI, projectionImages, null, null, null);
if(c != null){
c.moveToFirst();
Log.e(TAG, "am gasit " + c.getString(0) + " " + c.getString(1));
}
else{
Log.e(TAG, "No image");
}
but is not working.. I retrive just null for every entry!
Thank you!
Does it iterate over the amount of entries you see in the directory or do the numbers not match up?
The following works for me when searching for images in teh MediaStore
if (cursor.moveToFirst()) {
do {
int col = cursor.getColumnIndex(Images.Media.DESCRIPTION);
String description = cursor.getString(col);
if (new Long(item).toString().equals(description)) {
imageId = cursor.getPosition();
int dataColumn = cursor.getColumnIndex(Images.Media.DATA);
filePath = cursor.getString(dataColumn);
return true;
}
} while (cursor.moveToNext());
Try this one
Cursor cursorAlbum = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},MediaStore.Audio.Albums._ID+ "=" + album_id, null, null);
if(cursorAlbum != null && cursorAlbum.moveToFirst())
{
String uri = cursorAlbum.getString(cursorAlbum.getColumnIndex("album_art"));
cursorAlbum.close();
if(uri != null ) image.setImageURI(Uri.parse(uri));
}

Categories

Resources