得到允许误差java.lang.SecurityException异常:权限被拒绝在3.x的Andr​​oid设备,同时获得电子邮件附件名称 [英] Getting Permission error java.lang.SecurityException: Permission Denial on 3.x Android devices while getting email attachment name

查看:562
本文介绍了得到允许误差java.lang.SecurityException异常:权限被拒绝在3.x的Andr​​oid设备,同时获得电子邮件附件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题,在MYAPP打开电子邮件时,我做了它的发射方式为singleInstance。

I am facing issue in opening Email in MYApp when I made its launch mode to "singleInstance".

我附上样品的Andr​​oid项目从电子邮件附件中读取文件名,并在屏幕上显示出来。 工作正常情况下的onCreate,但在onNewIntent引发错误,当应用程序启动模式为singleInstance。

I have attached sample Android project which reads file name from email attachment and displays it on screen. Works fine in case of onCreate but throws error in onNewIntent when apps launch mode is singleInstance.

Launchmode.java

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, 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

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应用程序会在屏幕上显示的文件名。

Steps to reproduce 1)Install apk on device. 2)Go to gmail native app on device, open any attachment(office document) to view. 3)Choose LaunchMode app to complete action. 4)LaunchMode app will display file name on screen.

这工作正常,第一次(的onCreate流),但是当这个程序是开关在后台我再次尝试2,3,4步骤..应用程序崩溃,错误

This works fine for first time (onCreate flow) but when this app is switch in background and again I try 2,3,4 steps.. app crashes with error

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

我要解决这个问题的,我需要应用的单一实例,应收到电子邮件附件的名称了。 请让我知道如果我失去了一些东西。

I need to fix this as, I need to have single instance of Application and should get email attachment name too. Please let me know If I am missing something here.

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

My question here is why it work in flow of onCreate and it wont work in case of onNewIntent

请注意: 1)正常工作与2.x的手机 2)正常工作与单顶部发射方式。 3)在Gmail应用程序的某些更新。<一个href="http://www.google.com/support/forum/p/gmail/thread?fid=1728955d050a12bd0004a9339272acdb&hl=en">link位置:

Note: 1)Works fine with 2.x phones 2) Works fine with Single top launch mode. 3) Some updates on Gmail app.link here:

推荐答案

您可能有一个URI的权限,当您收到的意图,并没有使用您所要求的权限读取的文件名( READ_GMAIL WRITE_GMAIL )。该URI的许可才有效,直到您的应用程序完成() ES,所以你不会有它,当你尝试恢复。

You likely got a URI permission to read the file name when you received the intent and aren't using the permissions that you requested (READ_GMAIL and WRITE_GMAIL). The URI permission is valid only until your application finish()es, so you won't have it when you try to resume.

这是与您的经验一致的 - 它工作时的意图是新鲜的,而不是旧的。我认为, WRITE_GMAIL 是一个签名许可,我猜测 READ_GMAIL 是也。在这种情况下,没有什么可以做。 READ_ATTACHMENT 可能是一个更合适的权限,为您的要求。

That's consistent with your experiences - it works when the intent is fresh, but not old ones. I think that WRITE_GMAIL is a signature permission and I am guessing that READ_GMAIL is as well. In that case, there is not much you can do. READ_ATTACHMENT might be a more appropriate permission for you to request.

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

More on URI permissions: http://developer.android.com/guide/topics/security/permissions.html#uri

尝试删除使用-许可标签从你的清单,看看你是否有同样的经历。您也可以尝试当你通过检查其标志收到检查意图

Try removing the uses-permission tags from your manifest and see if you have the same experience. You can also try to examine the intent when you receive it by checking its flags.

checkCallingOrSelfUriPermission(documentUri , Intent.FLAG_GRANT_READ_URI_PERMISSION)

如果你返回0,你已经使用URI权限是。

If you get 0 returned, you have been using URI permissions.

这篇关于得到允许误差java.lang.SecurityException异常:权限被拒绝在3.x的Andr​​oid设备,同时获得电子邮件附件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆