获取android中联系人照片的文件系统路径

use*_*504 5 android android-contentprovider

嗨我有一个内容uri看起来像content://com.android.contacts/contacts/550/photo.

鉴于此内容uri,我需要获取物理文件的句柄.
我查看了stackoverflow上讨论的一些示例,但它们都不起作用.

有什么建议?

Uri*_*kel 2

这是一种获取字符串并创建该照片的文件并在进程结束时删除该文件的方法。像这样使用它: loadContactPhotoThumbnail(“content://com.android.contacts/contacts/550/photo”); 我从这里获取了大部分代码

     private String loadContactPhotoThumbnail(String photoData) {
    // Creates an asset file descriptor for the thumbnail file.
    AssetFileDescriptor afd = null;
    FileOutputStream outputStream = null;
    InputStream inputStream = null;
    // try-catch block for file not found
    try {
        // Creates a holder for the URI.
        Uri thumbUri;
        // If Android 3.0 or later
        if (Build.VERSION.SDK_INT
                >=
            Build.VERSION_CODES.HONEYCOMB) {
            // Sets the URI from the incoming PHOTO_THUMBNAIL_URI
            thumbUri = Uri.parse(photoData);
        } else {
        // Prior to Android 3.0, constructs a photo Uri using _ID
            /*
             * Creates a contact URI from the Contacts content URI
             * incoming photoData (_ID)
             */
            final Uri contactUri = Uri.withAppendedPath(
                    Contacts.CONTENT_URI, photoData);
            /*
             * Creates a photo URI by appending the content URI of
             * Contacts.Photo.
             */
            thumbUri =
                    Uri.withAppendedPath(
                            contactUri, Photo.CONTENT_DIRECTORY);
        }

    /*
     * Retrieves an AssetFileDescriptor object for the thumbnail
     * URI
     * using ContentResolver.openAssetFileDescriptor
     */
    afd = activity.getContentResolver().
            openAssetFileDescriptor(thumbUri, "r");

    FileDescriptor fdd = afd.getFileDescriptor();
    inputStream = new FileInputStream(fdd);
    File file = File.createTempFile("PhoneContactProvider", "tmp");
    file.deleteOnExit();
    outputStream = new FileOutputStream(file);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, len);
    }
    inputStream.close();
    outputStream.close();
    return "file://" + file.getAbsolutePath();
    } catch (Exception e) {
        App.logger().e(this.getClass().getSimpleName(), e.getMessage());
    }
    // In all cases, close the asset file descriptor
     finally {
        if (afd != null) {
            try {
                afd.close();
            } catch (IOException e) {}
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {}
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {}
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

注意:这不适用于 Android 版本 4.1.2 和 4.0.3 的模拟器