获取权限错误java.lang.SecurityException:获取电子邮件附件名称时,3.x Android设备上的权限拒绝

前端之家收集整理的这篇文章主要介绍了获取权限错误java.lang.SecurityException:获取电子邮件附件名称时,3.x Android设备上的权限拒绝前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我将其发布模式设置为“singleInstance”时,我正在打开MYApp中的电子邮件.

我附上了从电子邮件附件中读取文件名的示例Android项目,并将其显示在屏幕上.
在onCreate的情况下工作正常,但在应用程序启动模式为singleInstance时,会在onNewIntent中抛出错误.

Launchmode.java

  1. package your.namespace.launchmode;
  2.  
  3.  
  4. public class LaunchModeActivity extends Activity {
  5. private static final int OPEN_ACT = 2;
  6.  
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. String name = getAttachmetName(getIntent());
  13. if(null != name)
  14. {
  15. TextView textv = (TextView) findViewById(R.id.attachmentnm);
  16. textv.setText(name);
  17. }
  18. }
  19.  
  20. @Override
  21. protected void onNewIntent(Intent savedInstanceState)
  22. {
  23. super.onNewIntent(savedInstanceState);
  24. String name = getAttachmetName(savedInstanceState);
  25. if(null != name)
  26. {
  27. TextView textv = (TextView) findViewById(R.id.attachmentnm);
  28. textv.setText(name);
  29. }
  30. }
  31.  
  32.  
  33. private String getAttachmetName(Intent intent) {
  34. final Uri documentUri = intent.getData();
  35. if(null != documentUri){
  36. final String uriString = documentUri.toString();
  37. String documentFilename = null;
  38.  
  39.  
  40. final int mailIndexPos = uriString.lastIndexOf("/attachments");
  41. if (mailIndexPos != -1) {
  42. final Uri curi = documentUri;
  43. final String [] projection = new String[] {OpenableColumns.DISPLAY_NAME};
  44. final Cursor cursor = getApplicationContext().getContentResolver().query(curi,projection,null,null);
  45. if (cursor != null) {
  46. final int attIdx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
  47. if (attIdx != -1) {
  48. cursor.moveToFirst();
  49. documentFilename = cursor.getString(attIdx);
  50. }
  51. cursor.close();
  52. }
  53. }
  54. return documentFilename;
  55. }
  56. return null;
  57. }
  58.  
  59.  
  60. @Override
  61. protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  62. // TODO Auto-generated method stub
  63. super.onActivityResult(requestCode,resultCode,data);
  64.  
  65. if((resultCode == RESULT_OK) && (requestCode == OPEN_ACT))
  66. {
  67. Log.d("LaunchMode","Second activity returned");
  68. }
  69. }

}

AndroidManifest

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="your.namespace.launchmode"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>
  7. <uses-permission android:name="com.google.android.gm.permission.WRITE_GMAIL"/>
  8. <uses-permission android:name="com.google.android.providers.gmail.permission.READ_GMAIL"/>
  9. <uses-permission android:name="com.google.android.providers.gmail.permission.WRITE_GMAIL"/>
  10. <uses-sdk android:minSdkVersion="8" />
  11.  
  12. <application
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name" >
  15. <activity
  16. android:label="@string/app_name"
  17. android:launchMode="singleInstance"
  18. android:name=".LaunchModeActivity" >
  19. <intent-filter >
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. <intent-filter >
  24. <action android:name="android.intent.action.VIEW" />
  25.  
  26. <category android:name="android.intent.category.DEFAULT" />
  27. <!-- docx -->
  28. <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
  29. <!-- xlsx -->
  30. <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
  31. <!-- pptx -->
  32. <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
  33. <data android:mimeType="application/vnd.ms-excel" />
  34. <data android:mimeType="application/msword" />
  35. <data android:mimeType="application/vnd.ms-powerpoint" />
  36. <data android:mimeType="text/plain" />
  37. </intent-filter>
  38. </activity>
  39. </application>
  40. </manifest>

重现步骤
1)在设备上安装apk.
2)转到设备上的gmail本地应用程序,打开任何附件(office文档)进行查看.
3)选择LaunchMode应用程序来完成动作.
4)LaunchMode应用程序将在屏幕上显示文件名.

这可以第一次工作(onCreate流程),但是当这个应用程序在后台切换,我再次尝试2,3,4步骤..应用程序崩溃与错误

  1. E/DatabaseUtils(30615): java.lang.SecurityException: Permission Denial: reading com.google.android.gm.provider.MailProvider uri content://gmail-ls/qoconnect@gmail.com/messages/5/attachments/0.2/BEST/false from pid=32657,uid=10058 requires com.google.android.gm.permission.READ_GMAIL
  2. E/DatabaseUtils(30615): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:309)
  3. E/DatabaseUtils(30615): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:178)
  4. E/DatabaseUtils(30615): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
  5. E/DatabaseUtils(30615): at android.os.Binder.execTransact(Binder.java:339)
  6. E/DatabaseUtils(30615): at dalvik.system.NativeStart.run(Native Method)
  7. D/AndroidRuntime(32657): Shutting down VM

我需要解决这个问题,我需要一个应用程序的单一实例,并且应该获得电子邮件附件名称.
请让我知道如果我在这里遗漏的东西.

我的这个问题是为什么它工作在onCreate流,它不工作在情况下的onNewIntent

注意:
1)适用于2.x手机
2)使用单顶发射模式工作正常.
3)Gmail app.link here:上的一些更新

解决方法

当您收到意图并且不使用您请求的权限(READ_GMAIL和WRITE_GMAIL)时,您可能会获得URI权限来读取文件名. URI权限只有在您的应用程序完成()es之前才有效,所以当您尝试恢复时,您不会拥有该权限.

这与你的经历是一致的 – 它的意图是新鲜的,但不是旧的.我认为WRITE_GMAIL是一个签名许可,我猜测READ_GMAIL也是如此.在这种情况下,没有太多的事情可以做. READ_ATTACHMENT可能是更适合您请求的权限.

关于URI权限的更多信息:http://developer.android.com/guide/topics/security/permissions.html#uri

尝试从清单中删除uses-permission标签,看看是否有相同的体验.您还可以通过检查其标志来尝试检查意图.

  1. checkCallingOrSelfUriPermission(documentUri,Intent.FLAG_GRANT_READ_URI_PERMISSION)

如果你返回0,你一直在使用URI权限.

猜你在找的Android相关文章