即使Bitmap Uri不为null,从联系人获取位图也会失败

时间:2013-11-07 12:50:41

标签: android bitmap android-contacts filenotfoundexception

我试图从Bitmap获取完整大小的联系人图像,我知道这个图像并不总是可用...但是我发现Bitmap的uri不是null但是代码抛出了FileNotFoundException。为什么这是否会发生?我该怎么做才能解决这个问题?

我的代码

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

我在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)          

3 个答案:

答案 0 :(得分:0)

Solved。我使用AssetFileDescriptor而不是使用方法:

   context.getContentResolver().openInputStream(photoUri);

编辑: 一段时间后我回到了这个问题,我发现即使这样也行不通。有没有办法从ContactsProvider API获取全尺寸照片?

答案 1 :(得分:0)

以下是另一种获取大型照片的方法,这些照片可以回退到联系人缩略图:

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) {

                }               
            }
        }
    }

答案 2 :(得分: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;
        }