如何分享我在应用中拍摄的图像?

时间:2017-07-28 19:42:32

标签: android save share

我目前有一个应用程序,我正在拍照,我正试图通过应用程序直接分享它们。我遇到的问题是,当选择器出现并且我选择一个时,我会弹出错误的吐司并说“无法附加文件”。然后它只需要我回到应用程序,其他一切仍然有效。我假设这意味着我必须首先保存图像,但我不确定我是否正确地执行了此操作。我在网上找到了一些如何保存图像的例子,但我不确定这是问题还是共享意图是问题。

    final Button shareButton = (Button) findViewById(R.id.shareButton);
    shareButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            imageView.setDrawingCacheEnabled(true);
            Bitmap b = imageView.getDrawingCache();
            MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", "description");
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap image =  bitmapDrawable.getBitmap();
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
            startActivity(Intent.createChooser(shareIntent, "Share Image"));


        }
    });

我有“标题”和“描述”作为占位符,直到我可以使其工作。

1 个答案:

答案 0 :(得分:1)

尝试替换:

?funion

使用:

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath(‌​)));

This question can help you.

相关问题