NativeDecodeFileDescriptor中的NewFromFD失败 – Android 4.4

我使用 Android示例项目ContactsList( http://developer.android.com/shareables/training/ContactsList.zip)作为示例在我的应用程序中开发联系人Activity.
它在所有Android版本上都运行良好,但在Android 4.4中没有加载联系人图像,我收到以下错误
NewFromFD Failed in nativeDecodeFileDescriptor

执行此方法时会发生这种情况:

BitmapFactory.decodeFileDescriptor(fileDescriptor,null,options);

它总是返回null.

获取fileDescriptor本身,我使用此方法

private Bitmap loadContactPhotoThumbnail(String photoData,int imageSize) {
    if (!isAdded() || getActivity() == null) {
        return null;
    }

    AssetFileDescriptor afd = null;
    try {
        Uri thumbUri;

        if (Utils.hasHoneycomb()) {
            thumbUri = Uri.parse(photoData);
            Log.d("imageloader",photoData);
        } else {
            final Uri contactUri = Uri.withAppendedpath(Contacts.CONTENT_URI,photoData);

            thumbUri = Uri.withAppendedpath(contactUri,Photo.CONTENT_DIRECTORY);
        }

        afd = getActivity().getContentResolver().openAssetFileDescriptor(thumbUri,"r");
        Log.d("imageloader",afd.toString());

        FileDescriptor fileDescriptor = null;

        try{
            fileDescriptor = afd.getFileDescriptor();
            Log.d("imageloader",fileDescriptor.toString());
        } catch (NullPointerException e){
            e.printstacktrace();
        }

        if (fileDescriptor != null) {
            return ImageLoader.decodeSampledBitmapFromDescriptor(
                    fileDescriptor,imageSize,imageSize);
        }
    } catch (FileNotFoundException e) {

        if (BuildConfig.DEBUG) {
            Log.d(TAG,"Contact photo thumbnail not found for contact " + photoData
                    + ": " + e.toString());
        }
    } finally {
        if (afd != null) {
            try {
                afd.close();
            } catch (IOException e) {

            }
        }
    }

    return null;
}

而photoData是Contacts.PHOTO_THUMBNAIL_URI取自ContactsContract.Contacts.CONTENT_URI.

以下是上述代码的日志输出的一部分:

12-09 21:15:04.683: D/ImageCache(12531): Memory cache created (size = 6554)
12-09 21:15:05.024: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.024: D/imageloader(12531): content://com.android.contacts/contacts/296/photo
12-09 21:15:05.034: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[54]} start=0 len=-1}
12-09 21:15:05.034: D/imageloader(12531): FileDescriptor[54]
12-09 21:15:05.044: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.044: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.054: D/imageloader(12531): content://com.android.contacts/contacts/300/photo
12-09 21:15:05.064: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[54]} start=0 len=-1}
12-09 21:15:05.064: D/imageloader(12531): FileDescriptor[54]
12-09 21:15:05.074: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.084: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.084: D/imageloader(12531): content://com.android.contacts/contacts/318/photo
12-09 21:15:05.114: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[54]} start=0 len=-1}
12-09 21:15:05.114: D/imageloader(12531): FileDescriptor[54]
12-09 21:15:05.114: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.114: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.114: D/imageloader(12531): content://com.android.contacts/contacts/319/photo
12-09 21:15:05.124: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[54]} start=0 len=-1}
12-09 21:15:05.124: D/imageloader(12531): FileDescriptor[54]
12-09 21:15:05.124: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.124: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.124: D/imageloader(12531): content://com.android.contacts/contacts/320/photo
12-09 21:15:05.144: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[54]} start=0 len=-1}
12-09 21:15:05.144: D/imageloader(12531): FileDescriptor[54]
12-09 21:15:05.144: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.144: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.144: D/imageloader(12531): content://com.android.contacts/contacts/302/photo
12-09 21:15:05.154: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[55]} start=0 len=-1}
12-09 21:15:05.154: D/imageloader(12531): FileDescriptor[55]
12-09 21:15:05.154: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.164: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.164: D/imageloader(12531): content://com.android.contacts/contacts/301/photo
12-09 21:15:05.164: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[55]} start=0 len=-1}
12-09 21:15:05.164: D/imageloader(12531): FileDescriptor[55]
12-09 21:15:05.174: D/ImageLoader(12531): doInBackground - finished work
12-09 21:15:05.174: D/ImageLoader(12531): doInBackground - starting work
12-09 21:15:05.174: D/imageloader(12531): content://com.android.contacts/contacts/304/photo
12-09 21:15:05.184: D/imageloader(12531): {AssetFileDescriptor: {ParcelFileDescriptor: FileDescriptor[55]} start=0 len=-1}
12-09 21:15:05.184: D/imageloader(12531): FileDescriptor[55]
12-09 21:15:05.184: D/ImageLoader(12531): doInBackground - finished work

有人可以帮我解决这个问题吗?

解决方法

我有同样的问题,虽然我不知道是什么问题我找到了一个解决方案,适用于KitKat& JellyBean(4.2.2).您所要做的就是将文件作为InputStream而不是AssetFileDescriptor打开.我用过这段代码
private Bitmap loadContactPhotoThumbnail(String photoData) {
    InputStream is = null;
    try {
        Uri thumbUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            thumbUri = Uri.parse(photoData);
        } else {
            final Uri contactUri = Uri.withAppendedpath(
                    Contacts.CONTENT_URI,photoData);
            thumbUri = Uri.withAppendedpath(contactUri,Contacts.Photo.CONTENT_DIRECTORY);
        }

        is = getContentResolver().openInputStream(thumbUri);

        if (is != null) {
            return BitmapFactory.decodeStream(is);
        }
    } catch (FileNotFoundException e) {
        e.printstacktrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printstacktrace();
            }
        }
    }
    return null;
}

它与您在Display contact badge可以找到的代码相同,唯一的修改是它使用InputStream.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...