2016-01-19 54 views
1

我試圖附加圖像文件發送意圖(如Gmail等) 這裏是我的代碼:安卓:附加圖像以電子郵件的意圖 - 錯誤

public void mail(ReadableMap options, Callback callback) { 
Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("application/image"); 

    if (options.hasKey("subject") && !options.isNull("subject")) { 
    i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject")); 
} 

if (options.hasKey("body") && !options.isNull("body")) { 
    i.putExtra(Intent.EXTRA_TEXT, options.getString("body")); 
} 

if (options.hasKey("recipients") && !options.isNull("recipients")) { 
    ReadableArray r = options.getArray("recipients"); 
    int length = r.size(); 
    String[] recipients = new String[length]; 
    for (int keyIndex = 0; keyIndex < length; keyIndex++) { 
    recipients[keyIndex] = r.getString(keyIndex); 
    } 
    i.putExtra(Intent.EXTRA_EMAIL, recipients); 
} 

if (options.hasKey("attachment") && !options.isNull("attachment")) { 
    ReadableArray r = options.getArray("attachment"); 
    Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), r.getString(0))); 
//  Log.e("FILE", r.getString(0)); 
    i.putExtra(Intent.EXTRA_STREAM, uri); 
} 

Intent chooser = Intent.createChooser(i, "Send Mail"); 

chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
try { 
    reactContext.startActivity(chooser); 
} catch (Exception ex) { 
    callback.invoke("error"); 
} 
} 

數收益:

01-19 21:26:52.204 19326-19474/com.nevermind.app E/FILE: file:///storage/emulated/0/Pictures/e461e1a3-b000-4201-8371-00f5e61eca8c.jpg

圖像在設備上有效,但返回錯誤:無法附加空文件。其他電子郵件字段在GMail應用程序中運行良好。

下面是Android Studio中的輸出:

MultiPhoneWindowManager.shouldEnableLayoutInsetsBySoftInput:-1 01-19 21:35:14.864 1225-13155/system_process E/ClipboardUIManager: Failed to get ClipboardService 01-19 21:35:14.874 3840-3840/com.google.android.gm E/Gmail: Error adding attachment com.android.mail.utils.b: Cannot attach empty attachment at com.android.mail.ui.ComposeAttachmentTileGrid.a(SourceFile:62) at com.android.mail.compose.c.b(SourceFile:2098) at com.android.mail.compose.c.c(SourceFile:2228) at com.android.mail.compose.c.a(SourceFile:9157) at com.android.mail.compose.c.w(SourceFile:833) at com.android.mail.compose.c.onCreate(SourceFile:5904) at com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:165) at android.app.Activity.performCreate(Activity.java:6550) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) at android.app.ActivityThread.access$1000(ActivityThread.java:197) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6891) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

+0

當你說'記錄返回:'時,你說什麼日誌? Log.e()? – greenapps

+0

是的,這就是我的意思...... –

回答

2
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), r.getString(0))); 

更改爲:

Uri uri = Uri.parse(r.getString(0)); 

R.getSting已經包含完整路徑所以沒必要弄添加外部存儲。

+0

謝謝,這解決了它。 –

相關問題