2013-06-13 8 views
10

में एक सामग्री प्रदाता के साथ एक छवि साझा करें मैं एक एंड्रॉइड ऐप विकसित कर रहा हूं जो छवियों की एक गैलरी है जिसमें छवियों को स्मैशफ़ोन की स्क्रीन पर प्रदर्शित करने के लिए इंटरनेट से डाउनलोड किया जाता है। छवियों को एक समय में प्रदर्शित किया जाता है और एप्लिकेशन को प्रदर्शित छवि को साझा करने के लिए एक बटन होता है।एंड्रॉइड ऐप

कुछ स्टैक ओवरफ्लो पोस्ट में मिली दिशाओं के बाद मैंने संकेत दिया कि एक छवि साझा करने का सही तरीका सामग्री प्रदाता का उपयोग कर रहा था, मैंने निम्नलिखित कोड लागू किए हैं जो कुछ अनुप्रयोगों (जैसे ट्विटर, जीमेल) की छवियों को साझा करने के लिए काम करता है। ..) लेकिन दूसरों के लिए काम नहीं करता है (फेसबुक, याहू, एमएमएस ...)।

फिर मैं उम्मीद कर रहा कोड दिखाता हूं कि आप सभी अनुप्रयोगों में छवियों को साझा करने के लिए सही कार्यान्वयन पाने में मेरी सहायता कर सकते हैं।

शुरू में मैं साझा करने के लिए बटन प्रेस पर कब्जा:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (item.getItemId() == R.id.menu_share) { 

     // I get the image being displayed on the screen 
     View root = getView(); 
     ImageView imageView = (ImageView) root.findViewById(R.id.image); 
     Drawable imageToShareDrawable = imageView.getDrawable(); 

     if (imageToShareDrawable instanceof BitmapDrawable) { 

      // I convert the image to Bitmap 
      Bitmap imageToShare = ((BitmapDrawable) imageToShareDrawable).getBitmap(); 

      // Name of de image extracted from a bean property 
      String fileName = quote.getImage(); 

      // I keep the image in the folder "files" of internal storage application 
      TempInternalStorage.createCachedFile(fileName, imageToShare, getActivity().getApplicationContext()); 

      // I start the Activity to select the application to share the image after the intent Built with the method "getDefaultShareIntent" 
      startActivity(getDefaultShareIntent(fileName)); 
     } else { 
      Toast.makeText(getActivity().getApplicationContext(), "Please wait, the quote is being downloaded", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    return true; 
} 

आवेदन के आंतरिक भंडारण करने के लिए छवि को बचाने के लिए विधि निम्नानुसार है:

public static void createCachedFile(String fileName, Bitmap image, Context context) { 

    try { 
     File file = new File(context.getFilesDir(), fileName); 

     if (!file.exists()) { 
      FileOutputStream fos = new FileOutputStream(file); 
      image.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.flush(); 
      fos.close(); 
     } 
    } catch (Exception e) { 
     Log.e("saveTempFile()", "**** Error"); 
    } 
} 

विधि है कि आशय का निर्माण इसे साझा करने के लिए:

private Intent getDefaultShareIntent(String fileName) { 
    final Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Test text"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + File.separator + "img" + File.separator + fileName)); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    return shareIntent; 
} 

अंत में सामग्री प्रदाता कोड निम्नानुसार है:

public class CachedFileProvider extends ContentProvider { 

private static final String CLASS_NAME = "CachedFileProvider"; 

public static final String AUTHORITY = "com.example.appname.cachefileprovider"; 

private UriMatcher uriMatcher; 

@Override 
public boolean onCreate() { 
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 

    uriMatcher.addURI(AUTHORITY, "img/*", 1); 

    return true; 
} 

@Override 
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 

    String LOG_TAG = CLASS_NAME + " - openFile"; 

    Log.i(LOG_TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment()); 

    switch (uriMatcher.match(uri)) { 

    case 1: 

     String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment(); 

     ParcelFileDescriptor image = ParcelFileDescriptor.open(new File(fileLocation), ParcelFileDescriptor.MODE_READ_ONLY); 

     return image; 

    default: 
     Log.i(LOG_TAG, "Unsupported uri: '" + uri + "'."); 
     throw new FileNotFoundException("Unsupported uri: " + uri.toString()); 
    } 
} 

@Override 
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { 
    return 0; 
} 

@Override 
public int delete(Uri uri, String s, String[] as) { 
    return 0; 
} 

@Override 
public Uri insert(Uri uri, ContentValues contentvalues) { 
    return null; 
} 

@Override 
public String getType(Uri uri) { 
    return null; 
} 

@Override 
public Cursor query(Uri uri, String[] projection, String s, String[] as1, String s1) { 

    MatrixCursor c = null; 

    Log.i(">>>> projection", java.util.Arrays.toString(projection)); 

    String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment(); 

    File file = new File(fileLocation); 

    long time = System.currentTimeMillis(); 

    c = new MatrixCursor(new String[] { "_id", "_data", "orientation", "mime_type", "datetaken", "_display_name" }); 

    c.addRow(new Object[] { 0, file, 0, "image/jpeg", time, uri.getLastPathSegment() }); 

    return c; 
} 

@Override 
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { 
    return null; 
} 

}

मैं ने पाया है कि जब छवि कुछ अनुप्रयोगों साझा कर रहा है केवल विधि "क्वेरी" (ये हैं जहां कोड काम नहीं करता और मैं छवि साझा नहीं कर सकते) कहते हैं देखते हैं, जबकि अन्य लोग जो "क्वेरी" विधि भी कहते हैं, वे "ओपनफाइल" विधि भी कॉल करते हैं और ये काम करते हैं और मैं छवि साझा कर सकता हूं।

मुझे आशा है कि आप मेरी मदद कर सकते हैं, बहुत पहले आपको धन्यवाद।

+0

क्या आपको कोई समाधान मिला? जिस तरह से यह लिंक आपके लिए दिलचस्प हो सकता है: https://github.com/xperimental/BinaryContent – rekire

+0

क्या आपने सफलतापूर्वक अपनी छवि साझा की है, कृपया मेरी मदद करें कि मैं चित्रकारी फ़ोल्डर से छवि कैसे साझा कर सकता हूं? – Erum

+1

@ माइक क्या आप कृपया साझा कर सकते हैं कि आपके कोड में क्या गलती थी? – Erum

उत्तर

0

@Sun Ning-s टिप्पणी के अनुसार कुछ "शेयर लक्ष्य ऐप्स" यूआरआई-एस को "सामग्री: // .." से शुरू कर सकते हैं जिसे आपने कार्यान्वित किया है।

अन्य एप्लिकेशन संभाल फ़ाइल uri-s शुरू करने के साथ "file: // ...." तो तुम एक 2 हिस्सा menue

private Intent getFileShareIntent(String fullPathTofile) { 
    final Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fullPathTofile)); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    return shareIntent; 
} 

आप android app intentintercept करने के लिए उपयोग कर सकते हैं "फ़ाइल के रूप में शेयर" को लागू करने के लिए है पता लगाएं कि अन्य "शेयर स्रोत ऐप्स" क्या प्रदान करते हैं।