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

当我将其发布模式设置为“singleInstance”时,我正在打开MYApp中的电子邮件.

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

Launchmode.java

package your.namespace.launchmode;


public class LaunchModeActivity extends Activity {
    private static final int OPEN_ACT = 2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String name = getAttachmetName(getIntent());
    if(null != name)
    {
        TextView textv = (TextView) findViewById(R.id.attachmentnm);
        textv.setText(name);
    }
}

@Override
protected void onNewIntent(Intent savedInstanceState)
{
    super.onNewIntent(savedInstanceState);
    String name = getAttachmetName(savedInstanceState);
    if(null != name)
    {
        TextView textv = (TextView) findViewById(R.id.attachmentnm);
        textv.setText(name);
    }
}


private String getAttachmetName(Intent intent) {
    final Uri documentUri = intent.getData();
    if(null != documentUri){
    final String uriString = documentUri.toString();
    String documentFilename = null;


    final int mailIndexPos = uriString.lastIndexOf("/attachments");
    if (mailIndexPos != -1) {
        final Uri curi = documentUri;
        final String [] projection = new String[] {OpenableColumns.DISPLAY_NAME};
        final Cursor cursor = getApplicationContext().getContentResolver().query(curi,projection,null,null);
        if (cursor != null) {
            final int attIdx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            if (attIdx != -1) {
                cursor.moveToFirst();
                documentFilename = cursor.getString(attIdx);                
            }
            cursor.close();
        }
    }
    return documentFilename;
    }
    return null;
}


@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode,resultCode,data);

    if((resultCode == RESULT_OK) && (requestCode == OPEN_ACT))
    {
        Log.d("LaunchMode","Second activity returned");
    }
}

}

AndroidManifest

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.namespace.launchmode"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>
    <uses-permission android:name="com.google.android.gm.permission.WRITE_GMAIL"/>
    <uses-permission android:name="com.google.android.providers.gmail.permission.READ_GMAIL"/>
    <uses-permission android:name="com.google.android.providers.gmail.permission.WRITE_GMAIL"/>
    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:launchMode="singleInstance"
            android:name=".LaunchModeActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <!-- docx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
                <!-- xlsx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
                <!-- pptx -->
                <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" />
                <data android:mimeType="application/vnd.ms-excel" />
                <data android:mimeType="application/msword" />
                <data android:mimeType="application/vnd.ms-powerpoint" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>
</manifest>

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

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

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
E/DatabaseUtils(30615):     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:309)
E/DatabaseUtils(30615):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:178)
E/DatabaseUtils(30615):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
E/DatabaseUtils(30615):     at android.os.Binder.execTransact(Binder.java:339)
E/DatabaseUtils(30615):     at dalvik.system.NativeStart.run(Native Method)
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标签,看看是否有相同的体验.您还可以通过检查其标志来尝试检查意图.

checkCallingOrSelfUriPermission(documentUri,Intent.FLAG_GRANT_READ_URI_PERMISSION)

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

以上是来客网为你收集整理的获取权限错误java.lang.SecurityException:获取电子邮件附件名称时,3.x Android设备上的权限拒绝全部内容,希望文章能够帮你解决获取权限错误java.lang.SecurityException:获取电子邮件附件名称时,3.x Android设备上的权限拒绝所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。