Android 应用程序文件无法附加并作为电子邮件发送
Android app file cannot be attached and send as an email
"File could not be attached"
我正在尝试从我的应用程序中附加图像并将其作为附件通过电子邮件发送。但是,一旦我 select 来自图库的图像,应用程序就会崩溃。它曾经在我第一次执行该应用程序时工作,但现在由于某种原因它似乎不起作用。但后来我通过将代码行更新为:
来修复它
attachmentFile = cursor.getString(columnIndex);
Log.d("Attachment Path: "," " + attachmentFile);
从我的 Logcat 中,我找不到导致此问题的语句。
最初,我指的是这个问题,并尝试了其中给出的答案。
“java.lang.NullPointerException: println needs a message”。
"My DespatchActivity"
package com.example.despatch4.resiscafftest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.net.Uri;
import android.widget.Button;
import android.provider.MediaStore;
import android.database.Cursor;
import android.util.Log;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DespatchActivity extends AppCompatActivity {
private Activity activity;
Button Attachment;
String attachmentFile;
Uri URI = null;
int columnIndex;
private static final int PICK_FROM_GALLERY = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_despatch);
this.setTitle("Residential Scaffold - Despatch");
final EditText your_name = (EditText) findViewById(R.id.your_name);
final EditText company_name = (EditText) findViewById(R.id.company_name);
final EditText your_phone = (EditText) findViewById(R.id.your_ph);
final EditText your_jobno = (EditText) findViewById(R.id.your_jobno);
final EditText your_e_d = (EditText) findViewById(R.id.your_e_d);
final EditText job_details = (EditText) findViewById(R.id.job_details);
final EditText your_date = (EditText) findViewById(R.id.your_date);
Button email = (Button) findViewById(R.id.post_desp_message);
Attachment = (Button) findViewById(R.id.bt_attach);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = your_name.getText().toString();
String companyname = company_name.getText().toString();
String phone = your_phone.getText().toString();
String jobno = your_jobno.getText().toString();
String ed = your_e_d.getText().toString();
String jobdetails = job_details.getText().toString();
String date = your_date.getText().toString();
Intent sendEmail = new Intent(Intent.ACTION_SEND);
sendEmail.setType("plain/text");
sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"Zamil@residentialscaffold.com.au"});
sendEmail.putExtra(android.content.Intent.EXTRA_TEXT, "Name: " + name + '\n' + '\n' + "Company Name: " + companyname + '\n' + '\n'
+ '\n' + "Contact Number: " + phone + '\n' + '\n' + "Job Number: " + jobno + '\n' + '\n' + "Erect/Dismantle: " + ed + '\n' + '\n' + "Job Details: "
+ jobdetails + '\n' + '\n' + "Date Required: " + date);
if (URI != null) {
sendEmail.putExtra(Intent.EXTRA_STREAM, selectedImage);
}
startActivity(Intent.createChooser(sendEmail, "Send mail..."));
}
});
//attach images
Attachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFolder();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Uri selectedImage = null;
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
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]);
attachmentFile = cursor.getString(columnIndex);
//Log.e("Attachment Path:", " " , attachmentFile);
Log.d("Attachment Path: ","null" attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
public void openFolder()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}
}
一旦我尝试附加文件,我的 Logcat:
中就会发生这种情况
01-23 10:23:48.123 4334-4334/com.example.despatch4.resiscafftest
D/Attachment Path:: nullnull
电子邮件客户端已成功启动,并带有消息 "Couldn't attach file"。
提前致谢。
sendEmail.putExtra(Intent.EXTRA_STREAM, URI);
将该行更改为
sendEmail.putExtra(Intent.EXTRA_STREAM, data.getData());
使用 data.getData()
你在 onActivityResult()
中得到的那个。`
不要胡乱尝试获取路径。
而是直接使用 uri。
代码也少得多 ;-)。
"File could not be attached" 我正在尝试从我的应用程序中附加图像并将其作为附件通过电子邮件发送。但是,一旦我 select 来自图库的图像,应用程序就会崩溃。它曾经在我第一次执行该应用程序时工作,但现在由于某种原因它似乎不起作用。但后来我通过将代码行更新为:
来修复它 attachmentFile = cursor.getString(columnIndex);
Log.d("Attachment Path: "," " + attachmentFile);
从我的 Logcat 中,我找不到导致此问题的语句。
最初,我指的是这个问题,并尝试了其中给出的答案。
“java.lang.NullPointerException: println needs a message”。
"My DespatchActivity"
package com.example.despatch4.resiscafftest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.net.Uri;
import android.widget.Button;
import android.provider.MediaStore;
import android.database.Cursor;
import android.util.Log;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DespatchActivity extends AppCompatActivity {
private Activity activity;
Button Attachment;
String attachmentFile;
Uri URI = null;
int columnIndex;
private static final int PICK_FROM_GALLERY = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_despatch);
this.setTitle("Residential Scaffold - Despatch");
final EditText your_name = (EditText) findViewById(R.id.your_name);
final EditText company_name = (EditText) findViewById(R.id.company_name);
final EditText your_phone = (EditText) findViewById(R.id.your_ph);
final EditText your_jobno = (EditText) findViewById(R.id.your_jobno);
final EditText your_e_d = (EditText) findViewById(R.id.your_e_d);
final EditText job_details = (EditText) findViewById(R.id.job_details);
final EditText your_date = (EditText) findViewById(R.id.your_date);
Button email = (Button) findViewById(R.id.post_desp_message);
Attachment = (Button) findViewById(R.id.bt_attach);
email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = your_name.getText().toString();
String companyname = company_name.getText().toString();
String phone = your_phone.getText().toString();
String jobno = your_jobno.getText().toString();
String ed = your_e_d.getText().toString();
String jobdetails = job_details.getText().toString();
String date = your_date.getText().toString();
Intent sendEmail = new Intent(Intent.ACTION_SEND);
sendEmail.setType("plain/text");
sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"Zamil@residentialscaffold.com.au"});
sendEmail.putExtra(android.content.Intent.EXTRA_TEXT, "Name: " + name + '\n' + '\n' + "Company Name: " + companyname + '\n' + '\n'
+ '\n' + "Contact Number: " + phone + '\n' + '\n' + "Job Number: " + jobno + '\n' + '\n' + "Erect/Dismantle: " + ed + '\n' + '\n' + "Job Details: "
+ jobdetails + '\n' + '\n' + "Date Required: " + date);
if (URI != null) {
sendEmail.putExtra(Intent.EXTRA_STREAM, selectedImage);
}
startActivity(Intent.createChooser(sendEmail, "Send mail..."));
}
});
//attach images
Attachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFolder();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Uri selectedImage = null;
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
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]);
attachmentFile = cursor.getString(columnIndex);
//Log.e("Attachment Path:", " " , attachmentFile);
Log.d("Attachment Path: ","null" attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
public void openFolder()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}
}
一旦我尝试附加文件,我的 Logcat:
中就会发生这种情况 01-23 10:23:48.123 4334-4334/com.example.despatch4.resiscafftest
D/Attachment Path:: nullnull
电子邮件客户端已成功启动,并带有消息 "Couldn't attach file"。
提前致谢。
sendEmail.putExtra(Intent.EXTRA_STREAM, URI);
将该行更改为
sendEmail.putExtra(Intent.EXTRA_STREAM, data.getData());
使用 data.getData()
你在 onActivityResult()
中得到的那个。`
不要胡乱尝试获取路径。
而是直接使用 uri。
代码也少得多 ;-)。