2011-04-05 36 views
9

也許是一個簡單的問題:我想通過網絡分享一個位圖到twitter/facebook /等默認共享「意圖」。在Android上分享Bitmap()到twitter,facebook,郵件

的代碼,我發現在點「IDONTKNOW」與位圖來填充

 Intent sendIntent = new Intent(Intent.ACTION_SEND); 
     sendIntent.setType("image/jpeg"); 
     sendIntent.putExtra(Intent.EXTRA_STREAM, "IDONTKNOW"); 
     sendIntent.putExtra(Intent.EXTRA_TEXT, 
       "See my captured picture - wow :)"); 
     startActivity(Intent.createChooser(sendIntent, "share")); 

需求。 (this.bitmap)

我發現沒有辦法來處理這個不保存位圖到內部SD ..

問候

+0

這個問答是值得一讀http://stackoverflow.com/questions/9049143/android -share-intent-for-a-bitmap-is-it-possible-not-save-it-pre-sharing – Suragch 2015-05-11 04:16:56

回答

7

簡單地說,您可以將位圖轉換爲來自外部存儲的PNG。

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
File imageFile = new File(path, getCurrentTime()+ ".png"); 
FileOutputStream fileOutPutStream = new FileOutputStream(imageFile); 
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream); 

fileOutPutStream.flush(); 
fileOutPutStream.close(); 

然後,可以通過Uri.parse得到一個URI:

return Uri.parse("file://" + imageFile.getAbsolutePath()); 
+0

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); – 2016-01-10 11:38:51

1

好吧我對我自己,好像沒有辦法得到圖像URI不保存位圖到磁盤上,所以我用這個簡單的方法:

private Uri storeImage() { 
    this.storedImage = null; 
    this.storeImage = true; 
    // Wait for the image 
    while (this.storedImage == null && !this.stop) 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    this.storeImage = false; 
    FileOutputStream fileOutputStream = null; 
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    File file = new File(path, "cwth_" + getCurrentTime()+ ".jpg"); 
    try { 
     fileOutputStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
    this.storedImage.compress(CompressFormat.JPEG, JPEG_STORE_QUALITY, bos); 
    try { 
     bos.flush(); 
     bos.close(); 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return Uri.parse("file://" + file.getAbsolutePath()); 
} 
4

可能有點晚了,但你也可以做String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null);,如果你不關心它是如何存儲。

+0

此方法爲圖像創建縮略圖(作爲文檔)。 – wisemann 2016-04-12 08:50:58

-2

發送二進制內容 二進制數據是使用ACTION_SEND動作與設置適當的MIME類型和放置的URI相結合共享該數據位於額外的EXTRA_STREAM中。這通常用於共享的圖像,但可以用來共享任何類型的二進制內容:

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); 
shareIntent.setType("image/jpeg"); 
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))); 

http://developer.android.com/training/sharing/send.html

相關問題