2013-04-30 3 views
5

में संपर्क फोटो के लिए फ़ाइल सिस्टम पथ प्राप्त करें हाय मेरे पास एक सामग्री uri है जो content://com.android.contacts/contacts/550/photo जैसा दिखता है।एंड्रॉइड

मुझे इस सामग्री को दी गई भौतिक फ़ाइल में एक हैंडल प्राप्त करने की आवश्यकता है।
मैंने स्टैक ओवरफ्लो पर चर्चा के कुछ नमूने को देखा, लेकिन उनमें से कोई भी काम नहीं करता है।

कोई सुझाव?

उत्तर

1

यहां एक तरीका है जो स्ट्रिंग प्राप्त करता है और उस तस्वीर की एक फाइल बनाता है और प्रक्रिया समाप्त होने पर फ़ाइल को हटा देता है। इसे इस तरह प्रयोग करें: loadContactPhoto थंबनेल ("सामग्री: //com.android.contacts/contacts/550/photo");। मैं 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

के साथ