Security exception while reading attachments from gmail app in Android app

In our application we need to read attachments from email clients and upload it to the server. For the default email client for Android, everything works fine, but for the gmail application that addresses

java.lang.SecurityException: Permission Denial: opening provider com.google.android.gm.provider.MailProvider from ProcessRecord (pid=11298, uid=10068) requires com.google.android.gm.permission.READ_GMAIL or com.google.android.gm.permission.WRITE_GMAIL 

They even tried to grant Gmail read rights, but they didn’t work.

Observations

It works great for Nexus, but for samsung 4.2 and 4.1 devices it works fine if the activity is created for the first time, but if activity in the background is thrown above the above exceptions.

Trying to get the attachment file name using the code below.

  Cursor cursor = getContentResolver().query(openIntent.getData(), new String[] { MediaStore.MediaColumns.DISPLAY_NAME }, null, null, null); cursor.moveToFirst(); int nameIndex = cursor .getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME); if (nameIndex >= 0) { NCUtil.clickedImageTitle = cursor.getString(nameIndex); path = cursor.getString(nameIndex); } cursor.close(); 

My manifest file

  <activity android:name="com.ncomputing.vspacemobile.NCMainActivity" android:configChanges="orientation|keyboard|keyboardHidden|screenSize" android:excludeFromRecents="true" android:launchMode="singleTask" android:screenOrientation="sensorPortait" android:theme="@android:style/Theme.NoTitleBar" android:windowSoftInputMode="adjustPan" > <!-- For email attachments --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/pdf" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.ms-excel" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <data android:mimeType="application/vnd.ms-powerpoint" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <data android:mimeType="text/plain" /> <data android:mimeType="text/comma-separated-values" /> <data android:mimeType="application/rtf" /> </intent-filter> </activity> 

User rights

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> 

Please let us know how I can make it work, I need an activity trigger mode as singleTask and access to attachments.

+6
source share
3 answers

An intent filter requires both content and file scheme types, with the mimetype / octetstream application

 <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="file" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/> <data android:scheme="content" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/> 

according to the Android documentation: "If no schema is specified for the intent filter, all other URI attributes are ignored." When deleting a scheme and URI attributes, the only way to filter intentions is to use the Mime type, and we all know that there are no registered mime types in user file extensions.

For reference, URIs are of the form:

 scheme://host:port/path pathPrefix pathPattern 

So, without a circuit, it all falls. After discovering the above, I tried the obvious - use "*" for the circuit and even tried ". *". None of them worked. Hope someone else can atone for my trials. But I believe that this is due to the choice of the right scheme. Unfortunately, the only schemes that I know of are the content and the https https file, and none of them are a magic bullet.

 <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/*" host="*" android:pathPattern=".*.ext" android:scheme="content" /> </intent-filter> 

This intention will cause gmail to display the Download / Preview buttons. In fact, it will also make your application open when .ext files are sent as attachments for a regular email client.

A source:

Android will get the attached file name from the gmail application. Intent filter for downloading the application from gmail applications on Android

+5
source

The Android ActivityManager automatically clears permissions for each URI when the task of the called application is completed. URI permissions are typically set to Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET , the task clears when it runs in the background (so you don’t open the attachment viewer if you start Gmail from the start again). See docs for more details.

This is by design, and there is no way to "make it work." You must create your application in order to place this permission scheme well. You can read the entire attachment when starting the application, reset the temporary file / DB and continue downloading in the background using the service.

You don’t know which application you are creating, but usually the application that uploads your attachments to a third-party server sounds like a terrible idea in terms of privacy and security.

+3
source

You need these permissions to read email attachments ......

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="com.android.email.permission.READ_ATTACHMENT" /> 

Notes: READ_EXTERNAL_STORAGE assumed from WRITE_EXTERNAL_STORAGE , but it is best to add it as a best practice. INTERNET: An application can be retrieved from a provider that needs the Internet because it is email.

Tip. Make your code clean by creating a separate class for each mime type.

  <activity android:name=".KmlReader" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/title_activity_map" android:parentActivityName="com.gosylvester.bestrides.ImageTextListViewActivity" android:theme="@style/Theme.AppCompat" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.gosylvester.bestrides.ImageTextListViewActivity" /> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/vnd.google-earth.kml+xml" /> </intent-filter> </activity> 
0
source

All Articles