我如何与Intent.ACTION_SEND共享和保存图像和文本?

问题描述 投票:1回答:1

我想保存并与文本共享图像,并且我正在使用

i.putExtra(Intent.EXTRA_TEXT, shareMessages);
i.putExtra(Intent.EXTRA_STREAM,path);
i.setType("image/*");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

保存图像实际起作用!

但是结果是:设备上的Share Failed!

W/Bundle: Key android.intent.extra.STREAM expected Parcelable but value was a java.lang.String.  The default value <null> was returned.

在logcat中

也许您可以在看到下面的完整代码后为我提供帮助:

           cardTotal.setDrawingCacheEnabled(true);
            Bitmap b = cardTotal.getDrawingCache();

            int random = new Random().nextInt((100000 - 100)+ 1) + 100000;

            Long expense = getArguments().getLong("expense", 0);
            Long income = getArguments().getLong("income", 0);
            String shareMessages;

            File root = Environment.getExternalStorageDirectory();

            String path = root.toString() + "/" +textTitle.getText().toString()+random+ "mycarta.png";

            if (!income.equals(0L)){
                shareMessages = "Hey i get my income from" + textTitle.getText().toString() + "\n\n" + "Download MyCarta! Make your life easier, happier!";
            }else {
                shareMessages = "Hey i was doing " + textTitle.getText().toString() + "\n\n" + "Download MyCarta! Make your life easier, happier!";
            }

            try {
                b.compress(Bitmap.CompressFormat.PNG, 95, new FileOutputStream(path));
                System.out.println("SUCCESS============");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("ERROR==============");
            }

            FileInputStream finalPath = new FileInputStream(new File(path));
            finalPath.close();

            Intent i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_TEXT, shareMessages);
            i.putExtra(Intent.EXTRA_STREAM,path);
            i.setType("image/*");
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(i,"Share"));
        }

任何帮助或建议将不胜感激

java android android-studio android-intent stream
1个回答
0
投票

您必须使用Uri.parse(namepath.getAbsoultePath);

这是我的代码,并且正在正常工作

File root = Environment.getExternalStorageDirectory();
String location = "/" +textTitle.getText().toString()+random+"mycarta.png";

String path = root.toString() + location;
File imageDir = new File(root,location);

并在imageDir上使用Intent.EXTRA_STREAM,如下所示:

            Intent i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.setType("image/png");
            i.putExtra(Intent.EXTRA_TEXT, shareMessages);
            i.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageDir.getAbsolutePath()));
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(i,"Share"));
© www.soinside.com 2019 - 2024. All rights reserved.