我正在尝试发送附有文件的电子邮件,所以我已经下载了示例项目并正在处理它。问题是每次我收到相同的消息“无法附加文件(对不起我的英文) 到目前为止,我有这个。
public class MainActivity extends Activity implements OnClickListener {
EditText et_address, et_subject, et_message;
String address, subject, message, file_path;
Button bt_send, bt_attach;
TextView tv_attach;
private static final int PICK_IMAGE = 100;
Uri URI = null;
int columnindex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
bt_send.setOnClickListener(this);
bt_attach.setOnClickListener(this);
}
private void initializeViews() {
et_address = (EditText) findViewById(R.id.et_address_id);
et_subject = (EditText) findViewById(R.id.et_subject_id);
et_message = (EditText) findViewById(R.id.et_message_id);
bt_send = (Button) findViewById(R.id.bt_send_id);
bt_attach = (Button) findViewById(R.id.bt_attach_id);
tv_attach = (TextView) findViewById(R.id.tv_attach_id);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_attach_id:
openGallery();
break;
case R.id.bt_send_id:
try {
address = et_address.getText().toString();
subject = et_subject.getText().toString();
message = et_message.getText().toString();
String emailAddresses[] = { address };
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
emailAddresses);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
if (URI != null)
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
startActivity(emailIntent);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(filePathColumn[0]);
file_path = cursor.getString(columnindex);
// Log.e("Attachment Path:", attachmentFile);
tv_attach.setText(file_path);
URI = Uri.parse("file://" + file_path);
cursor.close();
}
}
答案 0 :(得分:0)
您对图库中返回的Uri
的处理过于复杂。您正在使用Uri
并对其进行处理以生成Uri
。
我认为您的onActivityResult()
方法应该更像:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
URI = data.getData();
}
}
这更好,因为传递"文件也不是好习惯://"如果外部电子邮件应用程序无法访问该文件,则Uri到外部应用程序。