我正在尝试发送带有附件的电子邮件。我有一个图像文件位于下载。但是当我选择图像然后应用程序崩溃。我试图调试并在onActivityResult
上设置断点,看到Result_code返回 - 1,所以我不知道我做错了什么。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
/**
* Get Path
*/
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]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
@Override
public void onClick(View v) {
if (v == btnAttachment) {
openGallery();
}
if (v == btnSend) {
try {
email = editTextEmail.getText().toString();
subject = editTextSubject.getText().toString();
message = editTextMessage.getText().toString();
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent
.putExtra(android.content.Intent.EXTRA_TEXT, message);
this.startActivity(Intent.createChooser(emailIntent,
"Sending email..."));
} catch (Throwable t) {
Toast.makeText(this,
"Request failed try again: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void openGallery() {
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);
}
这是输出
07-07 04:01:49.080: E/AndroidRuntime(1700): FATAL EXCEPTION: main
07-07 04:01:49.080: E/AndroidRuntime(1700): Process: com.example.mailapp, PID: 1700
07-07 04:01:49.080: E/AndroidRuntime(1700): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101,
result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:23 flg=0x1 }} to activity
{com.example.mailapp/com.example.mailapp.MainActivity}: java.lang.NullPointerException: println needs a message
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread.deliverResults(ActivityThread.java:3346)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread.access$1200(ActivityThread.java:135)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.os.Looper.loop(Looper.java:137)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-07 04:01:49.080: E/AndroidRuntime(1700): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 04:01:49.080: E/AndroidRuntime(1700): at java.lang.reflect.Method.invoke(Method.java:515)
07-07 04:01:49.080: E/AndroidRuntime(1700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-07 04:01:49.080: E/AndroidRuntime(1700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-07 04:01:49.080: E/AndroidRuntime(1700): at dalvik.system.NativeStart.main(Native Method)
07-07 04:01:49.080: E/AndroidRuntime(1700): Caused by: java.lang.NullPointerException: println needs a message
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.util.Log.println_native(Native Method)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.util.Log.e(Log.java:232)
07-07 04:01:49.080: E/AndroidRuntime(1700): at com.example.mailapp.MainActivity.onActivityResult(MainActivity.java:56)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.Activity.dispatchActivityResult(Activity.java:5435)
07-07 04:01:49.080: E/AndroidRuntime(1700): at android.app.ActivityThread.deliverResults(ActivityThread.java:3342)
07-07 04:01:49.080: E/AndroidRuntime(1700): ... 11 more
答案 0 :(得分:1)
我在想这就是问题所在:
Log.e("Attachment Path:", attachmentFile);
我认为attachmentFile
是null
。那里出了点问题。
也许这样做是为了确保:
Log.e("MyApp", "Attachment Path: " + attachmentFile);
此日志的第一个参数是您的标记。标记是您在logcat中对消息进行分组的内容。因此,System.out.prinln
会将此标记作为标记:com.example.app