【问题标题】:Share menu to save and share共享菜单以保存和共享
【发布时间】:2014-10-02 21:21:41
【问题描述】:

我在视图翻转器中有一个位图图像,我需要在操作栏中显示共享菜单。 单击共享图标后,应打开共享选项并保存到图库。 主题 - 分享和保存 选项 - 保存到图库,以及 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 共享和保存布局,它将显示上述选项,还是我必须创建自定义布局?

【问题讨论】:

    标签: android android-menu


    【解决方案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);
    

    【讨论】:

    • 我可以存储到内部存储而不是存储在外部存储中。共享选项会显示“保存到图库”吗?
    • 我添加了我需要为分享显示的布局。我希望你的回答会奏效。让我实施并让你知道。还有哪个更好保存,内部还是外部?
    • 我尝试通过 gmail 发送,但点击发送后显示“无法发送附件”
    • 那是我使用外部存储
    • 我尝试了外部存储,但是为了获得 View Flipper 的位图,我使用了 ImageView image = (ImageView) flipper.getCurrentView();位图位图 = ((BitmapDrawable)image.getDrawable()).getBitmap();但它失败了,蓝牙说未知文件
    猜你喜欢
    • 1970-01-01
    • 2012-09-04
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多