0

I am trying to get the full size Contact image from the Bitmap,I know that this image is not always available...however I found that the uri of the Bitmap was not null but the code is throwing a FileNotFoundException.Why does this happen and what can I do to solve this issue?

My code

   public Uri getFullSizePhotoUri(Uri contactUri)
{
    //get full size photo
    Log.d(TAG, "Getting full size photo uri");
    Uri photoUri=Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
    Log.d(TAG, "The full photo uri is "+photoUri.toString());
    return photoUri;
}

public Bitmap getFullSizeBitmapFromUri(Uri photoUri)
{   
    //returns full size picture...does not seem to work on all occasions

    AssetFileDescriptor afd=null;
    Bitmap bitmap=null;
    try
    {
        afd=context.getContentResolver().openAssetFileDescriptor(photoUri, "r");
        InputStream is=afd.createInputStream();
        bitmap=BitmapFactory.decodeStream(is);
        is.close();
    }
    catch(FileNotFoundException e)
    {
        Log.e(TAG, "File not found when trying to retrieve full sized photo using AssetFileDescriptor",e);
             return null;
    }
    catch(IOException e)
    {
        Log.e(TAG, "Error getting input stream from asset file descriptor",e);
           return null;
            }
    finally
    {
        try {
            afd.close();
        } catch (IOException e) {
            Log.e(TAG, "Error closing the asset file descriptor",e);
        }
    }
    return bitmap;  
}

I find this in my logcat:

   Contact id: 1003
   Lookup Uri: content://com.android.contacts/contacts/lookup/3266r1003-q33pq27pq57pq4Fp/1003
   Getting full size photo uri
   The full photo uri is content://com.android.contacts/contacts/1003/display_photo
   File not found when trying to retrieve full sized photo using AssetFileDescriptor
   java.io.FileNotFoundException: No photo file found for ID 0
   at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:145) at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:612)

  at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:628)
  at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:557)
  at com.example.newscancardapp.ContactsAdapter.getFullSizeBitmapFromUri(ContactsAdapter.java:99)   
  at com.example.newscancardapp.ContactsAdapter.bindView(ContactsAdapter.java:63)
  at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)          
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • 1
    If there is no image for contact 0 how are you expecting to get an image from something that is not there...? – JoxTraex Nov 08 '13 at 03:38
  • I set it to return null if the image could not be found...in the FileNotFoundException.Check the code now – vamsiampolu Nov 08 '13 at 03:53
  • From my understanding there is no way to test whether I get the full size photo using `PHOTO_URI(API level 11)` which seems to be populated either using the `PHOTO_FILE_ID(API level 14)`.Then there is PHOTO_ID(API level 5) which could hold a reference to the photo or not,if not fallback to `PHOTO_URI` or `PHOTO_THUMBNAIL_URI(API level 11)`.I use `2.3.3(API level 10)`.This is all confusing and perplexing and I use `DISPLAY_PHOTO(API level 14)`,it is gauranteed to give me a null value or crash below ICS(havent tested yet).Any solution you might know of.Wait,let me make it a seperate question. – vamsiampolu Nov 08 '13 at 04:14
  • @JoxTraex http://stackoverflow.com/questions/19851564/androidretrieving-image-from-contactsprovider-for-api-level-10 – vamsiampolu Nov 08 '13 at 04:25

3 Answers3

0

Solved.Instead of using the AssetFileDescriptor I have instead used the method:

   context.getContentResolver().openInputStream(photoUri);

EDIT: I came back to this problem after some time and I have found that even this does not work.Is there any way to obtain full sized photos from the ContactsProvider API?

vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
0

Here is another solution for getting large photos with fallback to contacts thumbnails:

public static Bitmap getContactBigPhoto(Uri contactUri, ContentResolver resolver) {     
        InputStream photoInputStream = null;
        try {                       
            photoInputStream = Contacts.openContactPhotoInputStream(resolver, contactUri, true);
            Bitmap photo = BitmapFactory.decodeStream(photoInputStream);            
            return photo;
        } catch (Exception e) {
            return null;
        } finally {
            if(photoInputStream != null) {
                try {
                    photoInputStream.close();
                } catch (IOException e) {

                }               
            }
        }
    }
Georgy
  • 364
  • 2
  • 14
0
// Solved, this code work in my case. with small and large size images
   public Bitmap getDisplayPhotoBitmap(long contactId) {
            Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
            InputStream photoInputStream =
                    ContactsContract.Contacts.openContactPhotoInputStream(ProspectActivity.this.getContentResolver(), contactUri);
            Bitmap bitmap = BitmapFactory.decodeStream(photoInputStream);
            if (bitmap != null) {
                return bitmap;
            }
            return null;
        }
jessica
  • 1,700
  • 1
  • 11
  • 17