i want to get a contact photo (if he has one) from the callLog.
now i know i can get the number and then query the contacts provider for a contact id.
however i want to know if there is a better way one that directly get the photo uri from the callLog.calls table.
what makes me believe it might be possible is the fact that inside the documentation i ran across 2 interesting fields:
1)CACHED_LOOKUP_URI -The cached URI to look up the contact associated with the phone number, if it exists.
2)CACHED_PHOTO_ID - The cached photo id of the picture associated with the phone number, if it exists.
now if it can be done how, and if it cant be done than i would like to know what those fields are used for,
thx
You can get its ID. Then you have to retrieve the actual image from ContactsContract.
imageDataRow = c.getInt(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_ID));
Cursor c = mContext.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[]{
ContactsContract.CommonDataKinds.Photo.PHOTO
}, ContactsContract.Data._ID + "=?", new String[]{
Integer.toString(imageDataRow)
}, null);
byte[] imageBytes = null;
if (c != null) {
if (c.moveToFirst()) {
imageBytes = c.getBlob(0);
}
c.close();
}
Bitmap photo = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
Mostly, I have used Glide to set an image on view.
SDK>=23
c.getString(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_URI));
SDK<23
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(num));
Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToNext()) {
image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
//Log.d(TAG, "image_uri "+image_uri);
}
if(cursor !=null)
cursor.close();
Related
so I'm pretty new to Android development. I'm trying to have the user select a song from their SD card or internal storage using a file manager and upload the file to a server. Right now, I'm just trying to access the name of the file that the user selected so that I can use it to access the contents of the file later and upload those contents.
I've looked at other posts concerning this and most of them tell you to query the content resolver and then use the cursor to grab the display name, but that does not always return the display name (this is mentioned in Google's guide to the SAF). It has not been returning the full file name, just part of it (the title of the song).
Here's my code that starts the intent:
Intent chooseIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooseIntent.addCategory(Intent.CATEGORY_OPENABLE);
chooseIntent.setType("audio/*");
startActivityForResult(chooseIntent, SELECT_SONG_FILE_REQUEST_CODE);
How I'm getting the display name now:
String fileName = null;
if (uri.getScheme().equals("content")) {
try (Cursor cursor = getContentResolver().query(uri, null, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
fileName = cursor.getString(
cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
Log.i(TAG, "Filename: " + fileName);
cursor.close();
}
}
}
How can I get the full file name rather than just the display name?
You also can use TITLE in Cursor as URI parameter to get the full name of file
use below code it
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.Audio.Media.TITLE, null, null, null);
Or You can use DATA to get Path of the file
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.Audio.Media.DATA, null, null, null);
All You have to do is let the user choose the music and after selecting with the use of DATA you have full path of song and with TITLE you have full name of file .
Next just upload it.
There are several things that you can do to improve the performance of the query
You are getting all the columns which can be painfully slow.
You can write code like the one below and then check what field you want to use. You will get all the data for all the audio files installed in the device. You can walk through this code in the debugger to see the data that you need.
{
Uri objUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = context.getContentResolver();
String[] entityColumns = {MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.AudioColumns.DISPLAY_NAME,
MediaStore.Audio.AudioColumns.TITLE};
cursor = cr.query(
objUri,
entityColumns,
null,
null,
null);
if (cursor != null && cursor.moveToFirst()) {
do {
String filePath = cursor.getString(cursor.getColumnIndex(entityCoumns[0]));
String fileName = cursor.getString(cursor.getColumnIndex(entityColumns[1]));
String fileTitle =
cursor.getString(cursor.getColumnIndex(entityColumns[2]));
} while (cursor.moveToNext());
cursor.close();
}
}
Alright, so the book I'm going through wants me to call a specific person.
I can retrieve the contact just fine, it's the whole "getting contact ID to find the phone number bit" that I'm having trouble with.
The app is crashing every time I select a contact.
I suppose I'm not sure how to properly navigate to the contact ID.
I am fairly new to Android and now I'm just getting completely lost, I haven't be able to find a solution on here that helps me.
Relevant Code:
}else if(requestCode == REQUEST_CONTACT) {
Uri contactsURI = data.getData();
String[] queryFields = new String[]{
ContactsContract.Contacts.DISPLAY_NAME
};
//Perform your query - the contactURI is like a "where"
//clause here
Cursor c = getActivity().getContentResolver().query(contactsURI, queryFields, null, null, null);
//Double-check that you actually got results
if (c.getCount() == 0) {
c.close();
return;
}
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
//Pull out the first column of the first row of data
//that is your suspects name
c.moveToFirst();
String suspect = c.getString(0);
mCrime.setmSuspect(suspect);
mSuspectButton.setText(suspect);
c.close();
}
}
I am crashing because of this part of the above code:
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
You have to first call
c.moveToFirst();
Then
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID))
In my android app when an incoming call i want to show my custom ui and i am able to do this.
No i want to check incoming number is from contacts or not.
Below is my code for doing so but it returns null for an incoming number which is stored in my contacts list.
public String findNameByNumber(String num){
Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(num));
String name = null;
Cursor cursor = mContext.getContentResolver().query(uri,
new String[] { Phones.DISPLAY_NAME }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME));
cursor.close();
callyName.setText(name+" Calling..");
}
return name;
}
and i have incoming call from a number say+917878787878 but in my contacts this contact is stored as name XYZ with number 78 78 787878,which is formated because between number there are space.and also try by excluding +91 but still it returns null.
So how can i find number which is stored in any format.Which may be stored with country code or not.
Thanks In advance.
Try this code instead (using PhoneLookup.CONTENT_FILTER_URI instead of Phones):
String res = null;
try {
ContentResolver resolver = ctx.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (c != null) { // cursor not null means number is found contactsTable
if (c.moveToFirst()) { // so now find the contact Name
res = c.getString(c.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
}
c.close();
}
} catch (Exception ex) {
/* Ignore */
}
return res;
As docs says ContactsContract.PhoneLookup: A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.
The following is my code for obtaining a thumbnail of a given image.
As of now, the exception that I'm getting is 'Cursor out of bounds'
I think that may be because I am not appending the image URL anywhere. I'm a little confused as to where to do that.
So 2 questions:
1. Where do I use the image URL of which I want to obtain a thumbnail
2. The for loop which is supposed to print column names prints nothing except the first statement 'COLUMN NAMES'
//get the corresponding thumbnail
String lastImageTakenPath = MyActivity.this.savedInstanceStateVariable.getString("lastImageTaken");
System.out.println("previous image is "+ lastImageTakenPath);
ContentResolver cr = getContentResolver();
if(cr != null){
String[] projection = { MediaStore.Images.Media._ID };
Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projection,null,null,null);
//Cursor cursor = cr.query(lastImageTakenURI, null, null, null, null);
//Activity.startManagingCursor(cursor);
if(cursor != null){
String[] columnNames = cursor.getColumnNames();
System.out.println("COLUMN NAMES");
for(int i=0;i<columnNames.length; i++){
System.out.println(columnNames[i]);
}
/* 1. get the id of the image
* 2. use this id in the call, getThumbnails on MediaStore.Images.Thumbnails to obtain the
thumbnail
3.set the imageview's src to this thumbnail */
int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) );
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
// Get original image ID
String url = uri.toString();
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
// Get (or create upon demand) the micro thumbnail for the original image.
thumbnailLastImage = MediaStore.Images.Thumbnails.getThumbnail(cr, originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND,null);
thumbnailImage.setImageBitmap(thumbnailLastImage);
}
else{
System.out.println("Cursor is NULL");
}
}
else{
Log.d(TAG,"ContentResolver is NULL");
}
I believe your test of if(cursor != null) is incorrect or at least insufficient. If the result of the query returns no thumbnails then you will still get a cursor where cursor.getCount() == 0 you may want to use that as your test.
Here is the code I am using to write a contact PHOTO. It works. If I am quick enough after writing it to the address book and go back and read the contact, I get my picture back.
But, after 10 seconds or so, in the debugger you can see an alarm fire and a thread startup and after that goes away if I go read the same contact back, the PHOTO was scaled down from roughly 400x400 down to 96x96.
It looks like there is a trigger firing on the backend to scale the PHOTO after it is written. Does anyone know a way to get around this or control it? Or is there a better way of writing a PHOTO that will not cause this scaling trigger?
I am doing this in Android 2.1 on Droid.
In trying to determine if I was writing the PHOTO correctly, I saw that there was a
ContactsContract.ContactsColumns interface with PHOTO_URI and PHOTO_THUMB_URI members, but I cannot find any way to get at it as the interface is protected and I cannot find any of the joins returning them. Does anyone how to use them?
Uri uri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = new String[] { BaseColumns._ID };
String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
String[] arguments = new String[] { "" + lContactID };
Cursor cursor = resolver.query(uri, projection, selection, arguments, null);
if (cursor != null && cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
long rawContactId = cursor.getLong(0);
int row = -1;
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Data._ID };
String selection = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
String[] params = new String[] { "" + rawContactId };
Cursor cursor = resolver.query(uri, projection, selection, params, null);
if (cursor != null && cursor.getCount() > 0)
{
if (cursor.moveToFirst())
{
row = cursor.getInt(0);
}
cursor.close();
}
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, pic);
values.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (row >= 0)
{
resolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
}
else
{
resolver.insert(ContactsContract.Data.CONTENT_URI, values);
}
}
cursor.close();
cursor = null;
}
It turns out that it is the gmail.com sync adapter that is scaling the photo.
if I turn off
Settings->Accounts&Syncs->gmail.com->“Sync Contacts” the problem goes away.
Why does the sync adapter need to do this on a PHOTO it did not put there?
If that were not enough, it is not even very consistent with how it does it:
If the gmail.com contact does not previously have a picture on the server, when I write the PHOTO to the address book it is not scaled. The picture shows up on the server.
However, if the gmail.com contact previously had a picture on the server, when I write the contact to the address book the picture I just wrote is scaled to 96x96. Once again the picture shows up on the server.
So, either the sync adapter has a bug with processing the original picture, or has a bug with the update of a picture, because it does not always scale the photo. Either way, it should not be scaling the picture :(