【问题标题】:Contact Picture doesn't show in list (KitKat)联系人图片未显示在列表中 (KitKat)
【发布时间】:2014-05-16 22:48:30
【问题描述】:

我试图在我的列表视图中显示联系人图像,并且我的代码一直有效,直到我发现 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

【问题讨论】:

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


    【解决方案1】:

    我完全不明白有什么问题。是检索还是显示? 在下面我给你一个代码剪断。它适用于 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();
            }
        }
    }
    

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

    亲切的问候

    【讨论】:

    • 我遇到的问题是获取联系人的缩略图或图像并将其显示在列表中。如果您在 KitKat 上的手机上尝试我在链接中提供的代码,它不起作用。我希望该代码能够正常工作。
    【解决方案2】:

    我通过更改以下方法解决了这个问题。我删除了 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多