NativeDecodeDefiledDescriptor-Android 4.4中的NewFromFD失败
我以Android示例项目ContactsList()为例,在我的应用程序中开发联系人活动。 它在所有Android版本上都能完美工作,但在Android 4.4中,联系人图像未加载,我得到以下错误:NativeDecodeDefiledDescriptor-Android 4.4中的NewFromFD失败,android,Android,我以Android示例项目ContactsList()为例,在我的应用程序中开发联系人活动。 它在所有Android版本上都能完美工作,但在Android 4.4中,联系人图像未加载,我得到以下错误: NewFromFD failed in nativeDecodeFileDescriptor 执行此方法时会发生这种情况: BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); 它总是返回null 要获取文件描述
NewFromFD failed in nativeDecodeFileDescriptor
执行此方法时会发生这种情况:
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
它总是返回null
要获取文件描述符本身,我使用以下方法:
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;
}
而照片数据是联系人的照片缩略图URI
取自联系人合同.联系人.内容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;
}
这与您可以在中找到的代码相同,唯一的修改是它使用
InputStream
显示日志以及代码中包含的日志输出。我已更新了我的代码,并包含了与这部分代码相关的日志部分。请告诉我是否需要我记录其他变量。非常感谢!这个解决方案对我也有效。我在安卓4.4和安卓2.3.3上进行了测试,效果很好。我认为它也适用于它们之间的所有版本。除了在三星设备上(在S4和表3上测试)外,效果非常好