【问题标题】:how do i share and save image and text together with Intent.ACTION_SEND?我如何与 Intent.ACTION_SEND 一起共享和保存图像和文本?
【发布时间】:2020-02-13 20:30:59
【问题描述】:

我想用文字保存和分享图像,我正在使用

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.

在日志中

也许你可以在看到下面的完整代码后帮助我:

           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】:

    你必须使用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);
    

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

                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"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多