2013-08-29 55 views
7

我正在將應用程序的以下代碼與共享共享。在可繪製文件夾中共享png圖像

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這是在繪製文件夾中。但圖像無法發送。是因爲在setType中,它是image/jpeg?我無法使用jpeg,因爲它失去了透明度。有人可以建議我如何與圖像分享?

這裏是我使用的圖像複製從繪製到SD卡的代碼:

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

不,它不起作用。它說所有項目都不能連接。我測試了fb,google +,gmail,消息應用程序。沒有工作。 – rick

+0

我試過了,它不工作 –

+0

對我不適用 – jonney

12

工作,我找到解決方案,我不應該複製圖像在SD卡上。那就是:

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我忘了添加捕捉NullPointerException(這裏http://stackoverflow.com/questions/12230942/why-images-media-insertimage-return-null解釋什麼時候有可能)。看到更新的答案。 –

+0

這使得drawable失去了透明度,你是否找到了處理這個問題的方法? –

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")); 

通過藍牙測試使用位圖。

工程就像一個魅力。

相關問題