我正在处理我的应用程序中的反馈功能,用户应该从库中选择一个图像并输入他的消息并按发送,在发送按钮上单击代码应该从框中获取文本(如下图所示并将其附加到电子邮件意图并发送整个内容,一切似乎都正常工作,直到我得到"无法附加空文件"吐司消息,这是我在代码中获得了多远
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);
}
}
答案 0 :(得分:2)
创建一个全局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)
答案 1 :(得分:1)
试试这种方式。
Uri selectedImageUri = data.getData();
String PathP;
Cursor cursor = getContentResolver().query(selectedImageUri, null, null, null, null);
if(cursor != null)
{
cursor.moveToFirst();
int idx =cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
PathP = cursor.getString(idx);
cursor.close();
}