2011-12-28 3 views
3

이미지 첨부 파일이있는 이메일을 보내고 싶지만 팝업 메뉴에서 Gmail 옵션을 선택할 때 항상 오류가 반환됩니다. 나는 그 이유를 찾지 못했습니다. 제발 나에게 맞는 솔루션을 제안하십시오.첨부 파일이있는 이메일을 보내는 중 오류가 발생했습니다.

코드 :

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Halosys Greetings"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,""); 
emailIntent.putExtra(Intent.EXTRA_STREAM, bm); 
emailIntent.setType("image/png"); 
startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

오류 스택 : 사전에

12-28 11:42:37.456: ERROR/AndroidRuntime(21930): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gm/com.google.android.gm.ComposeActivity}: java.lang.NullPointerException 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.os.Looper.loop(Looper.java:123) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at dalvik.system.NativeStart.main(Native Method) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930): Caused by: java.lang.NullPointerException 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.provider.Gmail$AttachmentOrigin.localFileExtras(Gmail.java:2194) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.ComposeArea.addAttachment(ComposeArea.java:732) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.ComposeArea.initFromExtras(ComposeArea.java:685) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.ComposeActivity.initFromExtras(ComposeActivity.java:1482) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.ComposeActivity.finishOnCreateAfterAccountSelected(ComposeActivity.java:1021) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at com.google.android.gm.ComposeActivity.onCreate(ComposeActivity.java:259) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
12-28 11:42:37.456: ERROR/AndroidRuntime(21930):  ... 11 more 

감사합니다.

+1

문제를 이해하려면 코드가 필요합니다. –

+0

내 편집 된 코드 블록을 참조하십시오 –

+0

여기에 bm은 무엇입니까? – Sujit

답변

3

어떻게 보내는지 모르지만이 코드는 정상적으로 작동합니다.

public void doSendFile() { 
    String fileName = "myFileName.txt"; 

    String externalStorageDirectory = Environment 
     .getExternalStorageDirectory().toString(); 
    String myDir = externalStorageDirectory + "/myDir/"; // the file will be in myDir 
    Uri uri = Uri.parse("file://" + myDir + fileName); 
    Intent i = new Intent(Intent.ACTION_SEND); 
    try { 
     myFileHandle.close(); // you may want to be sure that the file is closed correctly 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    i.setType("text/plain"); // as you can see I am sending a simple txt file here 
    i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
    i.putExtra(Intent.EXTRA_SUBJECT, "the subject text"); 
    i.putExtra(Intent.EXTRA_TEXT, "extra text body"); 
    Log.i(getClass().getSimpleName(), "logFile=" + uri); 
    i.putExtra(Intent.EXTRA_STREAM, uri); 

    try { 
     startActivity(Intent.createChooser(i, "Send mail...")); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(getBaseContext(), 
      "There are no email clients installed.", Toast.LENGTH_SHORT) 
       .show(); 
    } 
} 

는 당신이 실제로 외부 저장소에 파일을 만들 수 있도록 매니페스트에
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

을해야합니다.

P. 필자는 내부 저장소에서 직접 파일을 보내지 않았습니다.

2

사용이 코드 ..

Intent i = new Intent(Intent.ACTION_SEND); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
i.setType("image/jpg"); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg")); // Path to your image in sdcard. 
startActivity(i); 
2

저도 같은 문제를 겪고 난 잘못된 방법으로 Intent.EXTRA_STREAM를 사용하는 것을 발견했다. 이 문제를 해결

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg")); 

:

내가 좋아하는 뭔가를 사용하여 그것을 해결.

관련 문제