NewFromFD失败nativeDe codeFileDescriptor - Android 4.4系统 [英] NewFromFD failed in nativeDecodeFileDescriptor - Android 4.4

查看:206
本文介绍了NewFromFD失败nativeDe codeFileDescriptor - Android 4.4系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用的Andr​​oid示例项目ContactsList(<一href="http://developer.android.com/shareables/training/ContactsList.zip">http://developer.android.com/shareables/training/ContactsList.zip)作为一个例子来拓展人脉活动在我的应用程序。 它完美地工作在所有的Andr​​oid版本,但在Android 4.4系统不会被加载联系人图片,我得到了以下错误:

I have used Android sample project ContactsList (http://developer.android.com/shareables/training/ContactsList.zip) as an example to develop contacts Activity in my app. It is working perfectly on all Android versions, but in Android 4.4 the contact images are not being loaded and I get the following error:

 NewFromFD failed in nativeDecodeFileDescriptor

这发生时正在执行此方法:

This happens while this method is being executed:

BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

和它总是返回null。

and it always returns null.

要获得文件描述符本身我用这个方法:

To get the fileDescriptor itself I use this method:

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

and the photoData is the Contacts.PHOTO_THUMBNAIL_URI taken from ContactsContract.Contacts.CONTENT_URI.

这里是所提到的code日志输出的某一部分:

And here is some part of log output of the mentioned code:

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

任何人可以帮助我解决这个问题,好吗?

Can anybody help me with this problem, please?

推荐答案

我有同样的问题,虽然我不知道什么是我发现了一个解决方案,它适用于奇巧和功放的问题;软糖(4.2.2)。所有你需要做的就是打开文件作为的InputStream ,而不是 AssetFileDescriptor 。我用这个code:

I had the same problem, and although I do not know what is the problem I found a solution, which works on KitKat & JellyBean (4.2.2). All you have to do is opening the file as an InputStream instead of a AssetFileDescriptor. I used this code:

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

这是相同的code,你可以找到在<一个href="http://developer.android.com/training/contacts-provider/display-contact-badge.html#SetURIThumbnail">Display联系挂件,唯一的修改是,它使用的InputStream

It's the same code that you can find at Display contact badge, the only modification is that it uses InputStream.

这篇关于NewFromFD失败nativeDe codeFileDescriptor - Android 4.4系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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