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
Related
I have an idea about the widget that when I click on widget, the text & image will refresh randomly. I have done this with text, but my images are stored on Firebase and I want to take these random images and display them in an ImageView. So how I can do this?
Screenshot of my Firebase Storage:
Screenshot of my App:
As an alternative to #Bernd's answer: You could modify your image names to a standard naming scheme using incremental numbers. You could then retrieve the image URLs dynamically, like so:
Example image names:
image_0.jpg
image_1.jpg
image_2.jpg
image_3.jpg
Some example Java code to generate a random image filepath:
//The amount of images you have stored
int maxImages = 4; //Amount of images
Random random = new Random();
//Randomly generate a filepath to an image
String imageFilePath = "image_" + random.nextInt(maxImages) + ".jpg";
You can then use your generated imageFilePath with FireBase Storage's getDownloadUrl() to retrieve the proper download URL. You can then pass the URL to Glide, Picasso, or another image download library to load it into your ImageView.
Advantages
Only have to use Firebase Storage to achieve your goal
Less overhead on the database, don't have to maintain a list of images there
Disadvantages
You have to control the image names tightly, no custom image names
You have to have a fixed number of images
Could break if you delete an image without changing other image names
Retrieving the URL will throw an exception if the image couldn't be found (e.g. if the random number is out of bounds)
To randomly select an image from Firebase storage, you need to have a list of download url's of the files somewhere. As described in this answer, there is no Api to get a list of these pictures at the moment, so you will have to store the urls of them somewhere.
One possibility for this is to simply add the urls
of the files to Firebase database.
When you want to select a random image, you could go through the database,
and select a random url from this list.
To actually display them in the ImageView, you can use a library like Glide, to make this process easier. The FriendlyPix example App by Firebase, shows how to do this.
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
}
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.
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.
in android 2.1 how can i read phone contact image,if no contact image is there i need to get the default one the os provide.
Just use the convenience method openContactPhotoInputStream() of the ContactsContract.Contacts class which will return an InputStream containing the contacts photo if one is set. If no photo is set it will return null in which case you should try to set android.R.drawable.ic_contact_picture as your default image.
Right. After trying it myself it looks like you can't access the private Drawables which is even discouraged. Silly me.
However you can just navigate to your android-sdk/platforms/android-4/data/res/drawable directory and copy the ic_contact_picture to your projects drawable directory. Then if the method above returns null just set that drawable as your photo.