2013-08-29 8 views
7

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

private void socialShare() 
    { 
     Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon"); 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp"); 
     shareIntent.setType("image/jpeg"); 
     startActivity(Intent.createChooser(shareIntent, "Share from")); 
    } 

उपरोक्त कोड में रूप में, मैं png image जो drawable फ़ोल्डर में है डाल करने के लिए कोशिश कर रहा हूँ। लेकिन छवि भेजने में असमर्थ है। क्या ऐसा इसलिए है क्योंकि सेट टाइप में, यह छवि/जेपीईजी है? मैं जेपीईजी का उपयोग नहीं कर सकता, क्योंकि यह पारदर्शिता खो देता है। क्या कोई मुझे सुझाव दे सकता है कि छवि के साथ कैसे साझा करें?

यहाँ कोड है कि मैं drawable से sdcard के लिए चित्र को कॉपी करने के लिए उपयोग है:

String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; 
     File direct = new File(commonPath); 

     if(!direct.exists()) 
     { 
      if(direct.mkdir()) 
       { 
       Log.d("tag","directory created"); 
       //directory is created; 
       } 

     } 

     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.sharingimage); 
     OutputStream outStream = null; 
      File savingFile = new File(commonPath, "shareImage.png"); 
      if(!savingFile.exists()) 
      { 
       Log.d("tag","file is created"); 

      try { 
       savingFile.createNewFile(); 
       outStream = new FileOutputStream(savingFile); 
       bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
       outStream.flush(); 
       outStream.close(); 

       Log.d("tag","Saved"); 

       } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

       } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

       } 

      } 

उत्तर

1

आप सीधे एक uri साझा नहीं कर सकते से आप आंतरिक भंडारण एप्लिकेशन (निश्चित रूप से अपने अनुप्रयोग के रिसोर्सेज़ हमेशा रहेंगे आंतरिक भंडारण) में

इस को प्राप्त करने के दो तरीके हैं ..

  1. तो बाहरी संग्रहण में अपनी छवि को कॉपी वहाँ से साझा । this

  2. छवि साझा करने के लिए एक सामग्री प्रदाता लिखें। कि मैं समाधान नहीं मिला, जहां मैं चित्र को कॉपी नहीं किया जाना चाहिए उल्लेख Create and Share a File from Internal Storage

+0

आपके उत्तर के लिए धन्यवाद। लेकिन मैं इसे पहले तरीके से बनाने में असमर्थ हूं। – rick

+0

क्या आपने छवि को बाहरी स्टोरेज –

+0

नंबर में कॉपी किया है। मैं छवि की प्रतिलिपि बनाने में असमर्थ हूं। यह हमेशा फ़ाइल को अपवाद नहीं मिला फेंकता है। क्या आप कृपया मुझे कोड स्निपेट दिखा सकते हैं यदि आपके पास पहले से ही है? – rick

10

आप निम्न विधि का उपयोग कर साझा कर सकते हैं ...

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.setType("image/png"); 
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing"); 
startActivity(Intent.createChooser(shareIntent, "Send your image")); 

की कोशिश की और परीक्षण किया ... मेरे

+0

नहीं, यह काम नहीं करता है। यह कहता है कि सभी वस्तुओं को जोड़ा नहीं जा सकता है। मैंने एफबी, google +, जीमेल, मैसेजिंग ऐप के लिए परीक्षण किया। कुछ भी काम नहीं किया। – rick

+0

मैंने कोशिश की, यह काम नहीं कर रहा है –

+0

मेरे लिए काम नहीं कर रहा है – jonney

12

के लिए काम एसडी कार्ड पर। संदेश यह है:

try { 
     Uri imageUri = null; 
     try { 
      imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(), 
        BitmapFactory.decodeResource(getResources(), R.drawable.fragment_menu_logo), null, null)); 
     } catch (NullPointerException e) { 
     } 
     String text = getString(R.string.share_text); 
     // Launch the Google+ share dialog with attribution to your app. 
     Intent shareIntent = new PlusShare.Builder(mActivity) 
       .setType("image/*") 
       .setText(text) 
       .addStream(imageUri) 
       .getIntent(); 
     startActivity(shareIntent); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(mActivity, mActivity.getString(R.string.share_google_plus_not_installed), Toast.LENGTH_LONG).show(); 
    } 
+0

पर आज़माएं, यह पिछले समाधान पर काम करता है, धन्यवाद! –

+1

@RSenApps मैं पकड़ने के लिए नलपोइंटर अपवाद जोड़ने के लिए भूल गया (यहां http://stackoverflow.com/questions/12230942/why-images-media-insertimage-return-null यह बताता है कि यह संभव है)। जवाब अद्यतन देखें। –

+0

इससे ड्रॉबल अपनी पारदर्शिता खो देता है, क्या आपको इसे संभालने के लिए वैसे भी मिलते हैं? –

7
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); 
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg"; 
OutputStream out = null; 
File file=new File(path); 
try { 
    out = new FileOutputStream(file); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    out.flush(); 
    out.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
path=file.getPath(); 
Uri bmpUri = Uri.parse("file://"+path); 
Intent shareIntent = new Intent(); 
shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
shareIntent.setType("image/jpg"); 
startActivity(Intent.createChooser(shareIntent,"Share with")); 

यह पूरी तरह से मेरे लिए काम करता है, और इस लिखने की अनुमति

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

की आवश्यकता होगी इस लाइन लिखने की अनुमति के लिए प्रकट Android के लिए जोड़ें।

+0

एक आकर्षण की तरह काम करता है। धन्यवाद आदमी, आपने मेरा दिन बचाया :) – Ale

+0

बहुत बढ़िया .... !!! इतना आसान और मीठा जवाब .. !!! – Nils

3

पहले ऐड अनुमति

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

रेस

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.setType("image/jpeg"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
      String path = MediaStore.Images.Media.insertImage(getContentResolver(), 
        b, "Title", null); 
      Uri imageUri = Uri.parse(path); 
      share.putExtra(Intent.EXTRA_STREAM, imageUri); 
      startActivity(Intent.createChooser(share, "Select")); 

ब्लूटूथ के माध्यम से परीक्षण किया गया से उपयोग बिटमैप।

एक आकर्षण की तरह काम करता है।

संबंधित मुद्दे