2014-10-02 230 views
0

enter image description here我在視圖鰭狀肢中有一個位圖圖像,我需要在操作欄中顯示共享菜單。 一旦我點擊共享圖標,應該打開一個共享選項並保存到圖庫。 主題 - 分享和保存 選項 - 保存到相冊,和默認共享選項,如Gmail,視頻羣聊等共享菜單保存並共享

我能夠創建菜單:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.history_menu, menu); 
     MenuItem item = menu.findItem(R.id.menu_item_share); 
     return true; 
    } 

我能夠得到位圖從腳蹼形象:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.menu_item_share: 
       ImageView image = (ImageView) flipper.getCurrentView(); 
       Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 
       /*Need to know how to share and save image*/ 

       return true; 
      default: 
       return false; 
     } 
    } 

我需要知道我們是否有任何Android默認共享和保存的佈局,這將顯示以上選項,或做我必須創建一個自定義佈局?

回答

1
public static Bitmap getBitmapFromView(View view) { 
     // Define a bitmap with the same size as the view 
     Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), 
       view.getHeight(), Bitmap.Config.ARGB_8888); 
     // Bind a canvas to it 
     Canvas canvas = new Canvas(returnedBitmap); 
     // Get the view's background 
     Drawable bgDrawable = view.getBackground(); 
     if (bgDrawable != null) 
      // has background drawable, then draw it on the canvas 
      bgDrawable.draw(canvas); 
     else 
      // does not have background drawable, then draw white background on 
      // the canvas 
      canvas.drawColor(Color.WHITE); 
     // draw the view on the canvas 
     view.draw(canvas); 
     // return the bitmap 
     return returnedBitmap; 
    } 

    private String SaveImage(Bitmap finalBitmap) { 
     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + "/test"); 
     myDir.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     String fname = "Image-" + n + ".png"; 
     File file = new File(myDir, fname); 
     if (file.exists()) 
      file.delete(); 
     try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return file.getAbsolutePath(); 
    } 

以及分享

     Bitmap b = getBitmapFromView(root); 
         String s = SaveImage(b); 
         Intent sharingIntent = new Intent(
           Intent.ACTION_SEND); 
         Uri screenshotUri = Uri.parse(s); 
         sharingIntent.setType("image/*"); 
         sharingIntent.putExtra(Intent.EXTRA_STREAM, 
           Uri.fromFile(new File(s))); 
         sharingIntent.putExtra(Intent.EXTRA_TEXT, 
           "save to gallery"); 
         startActivity(sharingIntent); 
+0

我可以存儲不是在外部存儲器中存儲的內部存儲。分享選項會顯示「保存到圖庫」嗎? – user3722531 2014-10-02 13:07:43

+0

檢查編輯的喉嚨,也是可以在內部存儲 – 2014-10-02 13:09:21

+0

我添加了佈局,我需要顯示的份額..我希望你的答案會奏效。讓我執行並讓你知道。還有哪一個更好地保存,內部還是外部? – user3722531 2014-10-02 13:22:43