gpt4 book ai didi

java - Android - 将图像附加到电子邮件

转载 作者:行者123 更新时间:2023-12-01 21:21:34 25 4
gpt4 key购买 nike

我正在我的应用程序中开发反馈功能,用户应该从图库中选择一张图像并输入他的消息并按发送,在发送按钮上单击代码应该从框中获取文本(下图)并将其附加到电子邮件 Intent 并发送整个内容,一切似乎都工作正常,直到我收到“无法附加空文件”消息,这就是我在代码中的进展

 String rating = String.valueOf(RateBar.getRating());
String subject = Feedback.getText().toString();
String FileLocation = FilePathPreview.getText().toString();
String to ="EngNor.supp@gmail.com";
String message = UserInput.getText().toString();
String body = "Rating : "+rating+" out of 5"+
"\n "+message;
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, body);
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/"+FileLocation));

//need this to prompts email client only
email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));
<小时/>
public void grabImg(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String PathP = selectedImage.getPath().toString();
FilePathPreview.setText(PathP);
}
}

Feedback activity

最佳答案

创建一个全局 Uri 变量,然后将 uri 保存在 onActivityResult()

Uri uri = null;

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
uri = selectedImage; // here set the uri
String PathP = selectedImage.getPath().toString();
FilePathPreview.setText(PathP);
}
}

然后将相同的 uri 变量传递给

        //....
email.putExtra(Intent.EXTRA_STREAM, uri);
//....
//need this to prompts email client only
email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Choose an Email client :"));

更新:如果您仍然无法分享,请添加此标志

email.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

关于java - Android - 将图像附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079554/

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