0

我試圖發送電子郵件的意圖通過調用顯式GMAIL在我的應用程序。它在所有設備上工作正常,但現在我在Android 6棉花糖設備上發生崩潰。我收到的問題是:安卓棉花糖發送多個文件在Gmail中的意圖

12-14 00:02:55.365: E/AndroidRuntime(18570): Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexoicons, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo} 
12-14 00:02:55.365: E/AndroidRuntime(18570): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* flg=0x3 cmp=com.google.android.gm/.ComposeActivityGmail clip={image/* U:file:///storage/emulated/0/Mydir/myfile20161213105548.jpg} (has extras) } from ProcessRecord{cc9833c 18570:com.myappdr/u0a341} (pid=18570, uid=10341) not exported from uid 10050 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.os.Parcel.readException(Parcel.java:1620) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.os.Parcel.readException(Parcel.java:1573) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2677) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1509) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.Activity.startActivityForResult(Activity.java:3930) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.Activity.startActivityForResult(Activity.java:3890) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.Activity.startActivity(Activity.java:4213) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.Activity.startActivity(Activity.java:4181) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at com.myapp.SendGmail.onClick(SendGmail.java:222) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.os.Handler.dispatchMessage(Handler.java:102) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.os.Looper.loop(Looper.java:148) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at android.app.ActivityThread.main(ActivityThread.java:5461) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at java.lang.reflect.Method.invoke(Native Method) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-14 00:02:55.365: E/AndroidRuntime(18570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

下面是我在我的清單的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

這裏是如何我發送郵件在我的源代碼:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.mail_title)); 
emailIntent.setType("image/*"); 

ArrayList<Uri> uris = new ArrayList<Uri>(); 

for (Map.Entry<CartElement,Integer> c : SingletonShoppingCart.getInstance(null).get_Cart().entrySet()){ 
    if ((c.getKey().getImgPath() !=null) || (!c.getKey().getImgPath().isEmpty())){ 
     uris.add(Uri.fromFile(new File(c.getKey().getImgPath()))); 
    } 
} 

emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,strMailMsg); 
emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); 
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
startActivity(emailIntent); 
+0

請編輯您的問題併發布整個Java堆棧跟蹤。 – CommonsWare

+0

改爲使用ACTION_SEND。 – greenapps

+0

我已經添加了整個堆棧跟蹤。我目前可以在使用CianogenMod的OnePlusOne手機上重現它,但Developer Console中的崩潰來自Marshmallow。 –

回答

0

FLAG_GRANT_READ_URI_PERMISSION不適用於文件URI,例如從Uri.fromFile()生成的文件URI。您必須使用content:// URI,例如FileProvider

+0

我試過了,但如果我堅持只調用Gmail應用程序,它不起作用。 –

0

你有這樣的:

emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail"); 

刪除它,因爲這不是一個出口活動。該活動可用於啓動Gmail,但不適用於第三方啓動。並且,share where the user wants。並非所有Android用戶都使用Gmail。

長期來看,您還需要尋址Ian Lake's concern,因爲一旦將targetSdkVersion提升到24或更高,您的代碼將在Android 7.0及更高版本的設備上失敗。

+0

如果我希望用戶只明確使用Gmail應用,該怎麼辦? –