获取Android中联系人照片的文件系统路径 [英] get file system path for the contact photo in android

查看:188
本文介绍了获取Android中联系人照片的文件系统路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

有什么建议吗?

推荐答案

这里是一种获取字符串并为该照片制作文件并在处理结束时删除该文件的方法.像这样使用它: loadContactPhotoThumbnail("content://com.android.contacts/contacts/550/photo"); .我从此处

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

注意::此操作适用于具有Android版本4.1.2和4.0.3的仿真器

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

这篇关于获取Android中联系人照片的文件系统路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆