联系人图片未显示在列表中(KitKat)

时间:2014-05-12 19:34:00

标签: android android-contacts android-4.4-kitkat

我正在尝试在listview中显示联系人图片,我的代码一直有效,直到我发现nexus 5上的用户无法看到他们的联系人图片。我做了更多的测试,发现问题在KitKat(4.4)中越来越多。我使用了代码来显示此link的联系人。使用Google开发者网站上的代码,我无法显示联系人图片。

代码在链接中。如何才能在KitKat和其他版本中显示联系人图像?

以下是我在其他问题中看过的链接。

How to query Android MediaStore Content Provider, avoiding orphaned images?

Get/pick an image from Android's built-in Gallery app programmatically

Get real path from URI, Android KitKat new storage access framework

Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

2 个答案:

答案 0 :(得分:0)

我完全不明白这是什么问题。是要检索还是要显示?  在下面我给你一个代码剪切。它适用于4.4.2到版本3.在我的情况下,我推出了一个照片应用程序,例如一个画廊。

byte [] global_DATA15 = null;

从ContactsContract.Data.DATA15

中检索数据
                    Bitmap myBitMap = BitmapFactory.decodeByteArray(
                            global_DATA15, 0, global_DATA15.length);

                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                    myBitMap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

                    String path = Images.Media.insertImage(
                            myContext.getContentResolver(), myBitMap,
                            line4, "source: " + Constant.myClassName);


                    if (path != null) {
                        boolean isOK = true;
                        Uri myUri = null;
                        try {
                            myUri = Uri.parse(path);
                            convertedPath = getRealPathFromURI(myContext,
                                    myUri);
                        } catch (Exception expPhoto) {
                            isOK = false;
                            // .... do something 
                        }
                        if (isOK) {
                            Intent callPhoto = new Intent(
                                    Intent.ACTION_VIEW, myUri);
                            callPhoto.setDataAndType(myUri, "image/*");
                            startActivityForResult(callPhoto,
                                    Constant.photoCallRequestCode);
                        }

子程序

/**
 * returns the file path to an content uri
 * 
 * @param context
 * @param contentUri
 * @return path to picture
 */
public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null,
                null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

也许这有帮助,我不确定。

亲切的问候

答案 1 :(得分:0)

我通过更改以下方法解决了问题。我删除了AssetFileDescriptor并改为使用了InputStream。这是另一个问题,适用于我的代码。

/**
 * Decodes and scales a contact's image from a file pointed to by a Uri in the contact's data,
 * and returns the result as a Bitmap. The column that contains the Uri varies according to the
 * platform version.
 *
 * @param photoData For platforms prior to Android 3.0, provide the Contact._ID column value.
 *                  For Android 3.0 and later, provide the Contact.PHOTO_THUMBNAIL_URI value.
 * @param imageSize The desired target width and height of the output image in pixels.
 * @return A Bitmap containing the contact's image, resized to fit the provided image size. If
 * no thumbnail exists, returns null.
 */
private Bitmap loadContactPhotoThumbnail(String photoData, int imageSize) {

    // Ensures the Fragment is still added to an activity. As this method is called in a
    // background thread, there's the possibility the Fragment is no longer attached and
    // added to an activity. If so, no need to spend resources loading the contact photo.
    if (!isAdded() || getActivity() == null) {
        return null;
    }

    // Instantiates an AssetFileDescriptor. Given a content Uri pointing to an image file, the
    // ContentResolver can return an AssetFileDescriptor for the file.
    InputStream stream = null;

    // This "try" block catches an Exception if the file descriptor returned from the Contacts
    // Provider doesn't point to an existing file.
    try {
        Uri thumbUri;
        // If Android 3.0 or later, converts the Uri passed as a string to a Uri object.
        if (Utils.hasHoneycomb()) {
            thumbUri = Uri.parse(photoData);
        } else {
            // For versions prior to Android 3.0, appends the string argument to the content
            // Uri for the Contacts table.
            final Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, photoData);

            // Appends the content Uri for the Contacts.Photo table to the previously
            // constructed contact Uri to yield a content URI for the thumbnail image
            thumbUri = Uri.withAppendedPath(contactUri, Photo.CONTENT_DIRECTORY);
        }
        stream = getActivity().getContentResolver().openInputStream(thumbUri);

        return BitmapFactory.decodeStream(stream);
    } catch (FileNotFoundException e) {
        // If the file pointed to by the thumbnail URI doesn't exist, or the file can't be
        // opened in "read" mode, ContentResolver.openAssetFileDescriptor throws a
        // FileNotFoundException.
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Contact photo thumbnail not found for contact " + photoData
                    + ": " + e.toString());
        }
    } finally {
        // If an AssetFileDescriptor was returned, try to close it
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // Closing a file descriptor might cause an IOException if the file is
                // already closed. Nothing extra is needed to handle this.
            }
        }
    }

    // If the decoding failed, returns null
    return null;
}