获得从通讯录位图失败,即使位图乌里不为空 [英] Getting Bitmap from Contacts fails even though Bitmap Uri is not null

查看:240
本文介绍了获得从通讯录位图失败,即使位图乌里不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从位图得到全尺寸接触式图像,我知道,这一形象并不总是可用......然而,我发现,位图的URI是不为空,但code被扔FileNotFoundException.Why会出现这种情况,我能做些什么来解决这个问题呢?

我的code

 公众开放的getFullSizePhotoUri(URI contactUri)
{
    //获取全尺寸照片
    Log.d(TAG,获得全尺寸照片的URI);
    乌里photoUri = Uri.withAppendedPath(contactUri,ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
    Log.d(TAG的整幅照片的URI是+ photoUri.toString());
    返回photoUri;
}公共位图getFullSizeBitmapFromUri(URI photoUri)
{
    //返回全尺寸图片...似乎并没有在所有场合工作    AssetFileDescriptor AFD = NULL;
    位图位图= NULL;
    尝试
    {
        AFD = context.getContentResolver()openAssetFileDescriptor(photoUri,R)。
        InputStream为= afd.createInputStream();
        位= BitmapFactory.de codeStream(是);
        is.close();
    }
    赶上(FileNotFoundException异常E)
    {
        Log.e(TAG,E试图检索使用AssetFileDescriptor全尺寸的照片时,未找到文件);
             返回null;
    }
    赶上(IOException异常E)
    {
        Log.e(TAG,错误获取来自资产文件描述符输入流,E);
           返回null;
            }
    最后
    {
        尝试{
            afd.close();
        }赶上(IOException异常五){
            Log.e(TAG,错误关闭资产文件描述符,E);
        }
    }
    返回位图;
}

我觉得这在我的logcat的:

 联系ID:1003
   查找网址:内容://com.android.contacts/contacts/lookup/3266r1003-q33pq27pq57pq4Fp/1003
   获得全尺寸照片的URI
   完整的照片URI是内容://com.android.contacts/contacts/1003/display_photo
   试图检索使用AssetFileDescriptor全尺寸的照片时,未找到文件
   java.io.FileNotFoundException:找到ID 0无照片文件
   在android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:145)在android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:612)  在android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:628)
  在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:557)
  在com.example.newscancardapp.ContactsAdapter.getFullSizeBitmapFromUri(ContactsAdapter.java:99)
  在com.example.newscancardapp.ContactsAdapter.bindView(ContactsAdapter.java:63)
  在android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)


解决方案

下面是获得大的照片回退到接触缩略图另一种解决方案:

 公共静态位图getContactBigPhoto(URI contactUri,ContentResolver的解析器){
        InputStream的photoInputStream = NULL;
        尝试{
            photoInputStream = Contacts.openContactPhotoInputStream(解析器,contactUri,真正的);
            位图照片= BitmapFactory.de codeStream(photoInputStream);
            返回的照片;
        }赶上(例外五){
            返回null;
        } {最后
            如果(photoInputStream!= NULL){
                尝试{
                    photoInputStream.close();
                }赶上(IOException异常五){                }
            }
        }
    }

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)          

解决方案

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

                }               
            }
        }
    }

这篇关于获得从通讯录位图失败,即使位图乌里不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆