共享菜单以保存和共享

时间:2014-10-02 12:54:44

标签: android android-menu

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 个答案:

答案 0 :(得分: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);