gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:59:54 30 4
gpt4 key购买 nike

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

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

save image actually working !

但结果是:共享失败! 在设备上和

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

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

最佳答案

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

关于java - 我如何与 Intent.ACTION_SEND 一起共享和保存图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423814/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com