get file system path for the contact photo in android

对着背影说爱祢 提交于 2019-12-07 04:06:40

问题


Hi I have a content uri which looks like content://com.android.contacts/contacts/550/photo.

I need to obtain a handle to the physical file given this content uri.
I looked at some of the sample discussed on stackoverflow, but none of them work.

Any suggestions?


回答1:


Here is a method that gets the strings and makes a file of that photo and deletes the file when process ends. use it like this: loadContactPhotoThumbnail("content://com.android.contacts/contacts/550/photo");. I took most of the code from here

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

NOTICE: THIS WONT WORK ON EMULATOR WITH ANDROID VERSION 4.1.2 and 4.0.3



来源:https://stackoverflow.com/questions/16295887/get-file-system-path-for-the-contact-photo-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!