I have managed to get a ContactsContact.Directory working for the most part (http://stackoverflow.com/questions/7436969/contactscontract-directory-how-do-i-return-a-photo), and loading photos for the search result thumbnails. I can also view the contact details from the LOOKUP URI. But I am not able to pull up the user photo in the contact details.
My data does not reside in a database, but its all from a HTTPS REST Query.
To get around the image not being local, I created a second content provider which simply takes a URI, parses a parameter (url) and downloads an image (if not already cached on the sdcard) by way of overriding openFile (http://stackoverflow.com/questions/3883211/how-to-store-large-blobs-in-an-android-content-provider). That way when I pass the uri back as PHOTO_THUMBNAIL_URI
This works great and displays the images quite quickly in the results section of contacts. My issue is now that I can't load the photo in the contact details screen.
I understand that it is making a second query to my Directory Provider for LOOKUP, I am parsing all the fields, although it only asks for PHOTO_URI, not PHOTO_THUMBNAIL_URI, not a big deal as I am passing the same URI to my content provider.. But this time it does not seem to work correctly, as it does not even attempt to hit my photo provider.
I then decided to try and just locally parse & download the photo data and include the Byte[] data as PHOTO (Data15) in the Lookup Response, but that does not work either.
Any help would be greatly appreciated at this point.
static void addPhotoRow(MatrixCursor cursor, UdsProjection udsProjection, long contactId,
String lookupKey, String accountName, String displayName, String photoUri) {
UdsContactRow r = new UdsContactRow(
udsProjection, contactId, lookupKey, accountName, displayName);
r.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
r.put(Photo.PHOTO_ID, photoUri);
//r.put(Photo.PHOTO, getPhotoBytes(photoUri));
cursor.addRow(r.getRow());
Log.e("TAG","Adding photo row " + photoUri);
}
So I can shed some light on the results. Apparently the order in which you add your items to the cursor you return really matters. I moved the Photo mime type row in my cursor ahead of any of the details (such as phone number) and then it started working.. Hopefully this will help someone else from pulling their hair out for an afternoon.
Related
I have an activity with a listview and a custom adapter. Retrieving the contact information and the image is not the problem. I can not display an alternative image if there is not one for the contact.
Checking if the URI is NULL does not work for me.
I am retrieving the URI of the android contacts and the resulting URI have the format
content://com.android.contacts/contacts/XXX/photo
where XXX is a number. The resulting URI is only NULL for default contacts added like emergency numbers, so checking for NULL to replace the image with a placeholder image works only on those numbers. For all other contacts that have not set an image i want to check the filesize of the contact image.
File f = new File(uri.getPath());
long size = f.length();
String str = Long.toString(size);
Using the above commands results are always 0, it doesn´t matter if there is a photo or not.
How can i check the filesize correctly? or how can i check if the resulting photo is empty.
Thanks for helping
I encourage you to use Facebook Fresco library. You will be able to provide place holder image aswell as error image for image view
Use any famous image loaders like Universal image loader or picasso lib. This libs will handle every thing, its have some methods for putting error images. And you can directly put your contact URl in this libs.
They will support all kinds of url. you can check here
The idea to check if the file exists led me to this post where it was answered
detect if contact has photo
.getDrawable() == null
gets the correct result.
thanks for all comments
I am new to android and the concepts of programming, I'm a bit stuck on something atm and could do with a push in the right direction.
I am developing an app that has a function allowing users to capture an image, store that image in the app's file system, and store a reference to that image in an sqlite database. I explain this HERE, and got some good feedback as to how I would go about storing the reference in the database...I chose to just store the file name of the image.
The real issue here is that I do not know how to go about retrieving the image reference from the database and display the images in a grid view. The way I think this can work is to have a query something like:
IF (image name in database == image name in file system)
{
Display those images only
}
I don't know how to implement this, any help or insight will be greatly appreciated, thanks!
you can do something like this:
retrieve all the image names from the data base and then check if they exists on file system or not.
if(new File(FOLDERPATH + File.separator + file_name_in_db).exists()) {
// add file to list to be set in grid view adapter
}
Using a chooser:
Intent intent = new Intent();
intent.setType("audio/mp3");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, context.getResources().getString(R.string.select)), GET_URI);
I get an audio file. I take that Uri and save it in a local database to access later. I found that for a certain song selected the content uri would return a null value when you tried to create a MediaPlayer using it. I tried to get the content uri for that same song again to see what would happen, and sure enough:
Old, broken content uri: content://media/external/audio/media/2478
New, working content uri: content://media/external/audio/media/2487
Identical save for the last two digits being swapped. The method to obtain this Uri has not changed between my two attempts. Is this a known problem? My google and SO searched yielded no results. But this is a problem if content uris have a habit of becoming dyslexic, because there is no real way to remedy the problem short of re-obtaining the song's content uri, which must be done manually since there is no way to reference the song's file path with the wrong content uri.
Any ideas?
Dyslexic some of them are!
Some of the fields exposed by the Content Providers are cached. And so they are not guaranteed to be fresh. Since you did not specify the means by which you obtained this URI, I cannot tell if it is a cached value.
You can look into the code and figure it out yourself.
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/provider/MediaStore.java
I have to store image id for the image captured from camera.
Through this image id I can access its thumbnails and actual image considering those image resides on device already.
I have implemented this with image picker intent, but don't know if this is possible with Camera intent. The data when printed gives null, which for me doesn't seems to be notified to Mediastore.
Any idea or solution is appreciated.
Anyways, for a while I am implementing a workaround until I find the conventional way of doing it.
Currently my self solution is, to get a list of all the image IDs of both stages, before taking photo and after taking the photo. The new ID in new list is the ID I am looking for, when filtered with old list.
below is my code snippet.
private String getDiscrepantId(ArrayList<String> old_lst, ArrayList<String> new_lst){
for(int i=0; i<old_lst.size(); i++){
if(new_lst.contains(old_lst.get(i))){
new_lst.remove(old_lst.get(i));
}
}
return new_lst.get(0);
}
I hope there must be some better way to achieve this, which I believe someone will share it soon.
When I take a picture with Android's camera app, it is inserted in the gallery with a name identical to it's creation date/time, like "2010-02-04 16.36.15.jpg"
In my app i take my own pics, and insert to the gallery them using
android.provider.MediaStore.Images.Media.insertImage(
ContentResolver cr, Bitmap source, String title, String description);
However, in the gallery they appear with a name that seems to be the time stamp of their creation in millis, like "1265323665851.jpg" , no matter what i put in the tittle argument.
I need them to have the same name they would get from Android's camera app, so that they will be properly ordered.
Is there a way to do so?
Thanks in advance.
Old question. I eventually found out the order of the pictures was about a field in the database.