I'm trying to send an e-mail from my Android App. With the click on a button, gmail should open and show a new email with my previously defined recipient, subject and email body. So far I've tried sending the Intent.ACTION_VIEW as well as Intent.ACTION_SENDTO. Both show my draft with the recipient only. Both subject and message are being opressed. Weird thing is when using the emulator, it works just fine. Also was trying to lock at the android errorlog. Seems like i don't have permission. Is it really a permission problem or could it be something else?
I'd really appreciate any help
cheers
Here is my code:
sending email via ACTION_VIEW
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:" + to));
intent.putExtra(intent.EXTRA_SUBJECT, subject);
intent.putExtra(intent.EXTRA_TEXT, message);
mainActivity.startActivity(intent);
sending email via ACTION_SENDTO
Intent email = new Intent(Intent.ACTION_SENDTO);
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
mainActivity.startActivity(Intent.createChooser(email, "Choose an Email client :"));
error message from logcat
2019-12-13 01:30:35.172 29268-29268/? E//system/bin/webview_zygote32: failed to make and chown /acct/uid_99044: Permission denied
2019-12-13 01:30:35.172 29268-29268/? E/Zygote: createProcessGroup(99044, 0) failed: Permission denied
2019-12-13 01:30:35.206 29289-29289/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.226 29296-29296/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.355 29268-29268/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Regular.ttf
2019-12-13 01:30:35.356 29268-29268/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Bold.ttf
2019-12-13 01:30:35.356 29268-29268/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:35.356 29268-29268/? E/Typeface: Unable to load Family: null:und-Khmr
2019-12-13 01:30:35.484 29268-29268/? E/Typeface: Error mapping font file /system/fonts/LGAka_Light.ttf
2019-12-13 01:30:35.484 29268-29268/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:35.484 29268-29268/? E/Typeface: Unable to load Family: lg-lgaka:null
2019-12-13 01:30:35.816 29342-29342/? E//system/bin/webview_zygote32: failed to make and chown /acct/uid_99045: Permission denied
2019-12-13 01:30:35.816 29342-29342/? E/Zygote: createProcessGroup(99045, 0) failed: Permission denied
2019-12-13 01:30:35.842 29354-29354/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:35.864 29367-29367/? E/asset: setgid: Operation not permitted
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Regular.ttf
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Error mapping font file /system/fonts/NotoSansKhmer-Bold.ttf
2019-12-13 01:30:36.139 29342-29342/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:36.139 29342-29342/? E/Typeface: Unable to load Family: null:und-Khmr
2019-12-13 01:30:36.362 29342-29342/? E/Typeface: Error mapping font file /system/fonts/LGAka_Light.ttf
2019-12-13 01:30:36.362 29342-29342/? E/Minikin: Could not get cmap table size!
2019-12-13 01:30:36.362 29342-29342/? E/Typeface: Unable to load Family: lg-lgaka:null
2019-12-13 01:30:36.523 4349-4359/? E/GBMv2: FPS Scaler: EXP
2019-12-13 01:30:36.602 29342-29342/? E/WebViewFactory: can't load with relro file; address space not reserved
2019-12-13 01:30:37.058 29220-29220/? E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad; source: file:///android_asset/draft_editor_gmail_compiled.js at 89
2019-12-13 01:30:37.146 29220-29220/? E/Gmail: Gmail:EditWebView JS Console: b/119949571:draft.editor.onLoad is finished; source: file:///android_asset/draft_editor_gmail_compiled.js at 90
I think we had the same issue. Android API 29 introduced some improvements about sending data to other apps. See more details here: Sending simple data to other apps
Here is the solution that works for me.
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"address#mail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "The subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "The email body");
emailIntent.setSelector( selectorIntent );
activity.startActivity(Intent.createChooser(emailIntent, "Send email..."));
In few words, with this you are asking for the Android standard app chooser and, in addition, you specify that you want to send an email. So, as result, email clients will appear only.
If user has one email client installed only, the intent will redirect to it instantly.
Hope this helps you too.
try out this code, it worked for me.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject here");
intent.putExtra(Intent.EXTRA_TEXT,"Body Here");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
also add intent filter in android manifest.
<activity ...>
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
In order to get it to work on Samsung and Pixel devices, we had to add the parameters on both the url and the extras
val email = "xxxx#xxxx.com"
val subject = "xxxx"
val body = "xxxx"
val selectorIntent = Intent(Intent.ACTION_SENDTO)
val urlString = "mailto:" + Uri.encode(email) + "?subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body)
selectorIntent.data = Uri.parse(urlString)
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, body)
emailIntent.selector = selectorIntent
startActivity(Intent.createChooser(emailIntent, "Send email"))
Our old code for emails stopped working some days ago.
It was the following:
public static void shareTextToEmail(Context context, String[] email, String subject, String text)
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + TextUtils.join(",", email)));
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
try {
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.share_email_title)));
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(context, context.getString(R.string.share_no_intent_handler_found), Toast.LENGTH_SHORT).show();
}
}
I've adopted it according to the Zak.Antonio answer:
public static void shareTextToEmail(Context context, String[] email, String subject, String text)
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, text);
emailIntent.setSelector(selectorIntent);
try {
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.share_email_title)));
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(context, context.getString(R.string.share_no_intent_handler_found), Toast.LENGTH_SHORT).show();
}
}
The key points are:
Replace Intent.ACTION_SENDTO with Intent.ACTION_SEND in emailIntent
Move Intent.ACTION_SENDTO to a selectorIntent
Do not put emails in intent data, put them only in extras at Intent.EXTRA_EMAIL
If your app targets Android 11 (API level 30) or higher then we require to add :
<queries> element in the app's manifest file (Because of
Package visibility changes in Android 11)
Set the queries in the Manifest file like this:
<manifest package="com.example.app">
...
<!-- Package visibility -->
<queries>
<!-- Mail -->
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent>
</queries>
...
</manifest>
Kotlin method to open Email apps :
fun composeEmail(recipient: String, subject: String, body: String) {
val selectorIntent = Intent(Intent.ACTION_SENDTO).apply{
data = Uri.parse("mailto:") // only email apps should handle this
}
val emailIntent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, body)
selector = selectorIntent
}
if (emailIntent.resolveActivity(packageManager) != null) {
startActivity(emailIntent)
}
}
This answer worked for me using parsing using Uri
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:?subject=" + subject + "&to=" + to));
context.startActivity(emailIntent);
try this code
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.setType("text/plain")
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("jon#example.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text")
val packageManager = packageManager
val activities = packageManager.queryIntentActivities(emailIntent, 0)
val isIntentSafe = activities.size > 0
if (isIntentSafe) {
startActivity(emailIntent);
}else{
Log.d("MainActivty","Email App not installed");
}
Neither adding Extras to the SENDTO intent nor using a selectorIntent seemed to work on a Samsung running Android 10. Uri encoding the email address, subject, and body seemed to work best across multiple devices running Android 10 and below. The EmailIntentLibrary was a big help in ironing out the finer points of getting the URI encoding to work for complex body content.
val email = Uri.encode("xxxx#xxxx.com")
val subject = Uri.encode("xxxx")
val body = Uri.encode("some body. one two & more. \n new line \n &%>?")
val uri = "mailto:$email?subject=$subject&body=$body"
val intent = Intent(Intent.ACTION_SENDTO)
intent.type = "text/plain"
intent.data = Uri.parse(uri)
startActivity(intent)
If you're ok with having quite a few options in the chooser, the example below from the Android Development Guide does work (the ACTION_SENDTO example meant to filter those options did not work for me)
fun composeEmail(addresses: Array<String>, subject: String, attachment: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "*/*"
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_STREAM, attachment)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
I had issues with the other solutions, here is what worked for me:
val selectorIntent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
}
val emailIntent = Intent(Intent.ACTION_SEND).apply {
selector = selectorIntent
putExtra(
Intent.EXTRA_EMAIL,
arrayOf(resources.getString(R.string.support_email_address))
)
putExtra(
Intent.EXTRA_SUBJECT,
resources.getString(R.string.support_email_subject)
)
putExtra(
Intent.EXTRA_TEXT,
resources.getString(R.string.support_email_body)
)
putExtra(
Intent.EXTRA_STREAM,
latestLogFileUri
)
}
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (selectorIntent.resolveActivity(packageManager) != null) {
Handler(Looper.getMainLooper()).postDelayed({
startActivity(
Intent.createChooser(
emailIntent,
resources.getString(R.string.support_email_chooser_title)
)
)
}, 2000)
}
and don't forget to put this into your AndroidManifest.xml:
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
</queries>
Related
I'm trying to delete song file but it is unable to delete and throws an exception of permission denied but permissions are already granted using permission_handler and required storage permissions are added in AndroidManifest.xml as well. I have also checked path is valid and file exist before delete which return true. Would anyone help me to solve this.
Error on file.delete() line.
Unhandled Exception: FileSystemException: Deletion failed, path = '/storage/0E15-2E06/Music/Call Sound Effect.mp3' (OS Error: Permission denied, errno = 13)
AndroidManifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:label="Example"
android:name="${applicationName}"
android:icon="#mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true" >
Code snippet:
if (await Permission.storage.request().isGranted) {
// Either the permission was already granted before or the user just granted it.
// permission was granted
if (song != null) {
String? path = song.data;
File file = File(path);
bool isExist = await file.exists();
if (isExist) {
await file.delete(recursive: true);
}
}
}
You have to use the permission_handler(https://pub.dev/packages/permission_handler) package and have to mention some permissions in the Androidmanifest.xml file.
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also, you have to ask Permission of storage before doing storage related operation(In your case deletion of audio file).
final _permissionStatus = await Permission.storage.request();
if(_permissionStatus = PermissionStatus.isGranted){
/// Delete audio file
}else{
/// Permission Denied.
}
I'm trying to share a image with plugin share flutter, but I'm getting the following error:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/.../cache/share/image-777.jpg
AndroidManifest.xml:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_path"/>
</provider>
flutter:
final urlImage =
'https://images.tokopedia.net/img/cache/500-square/hDjmkQ/2021/9/13/343063bb-f7c5-468d-9602-9b06ced70bdb.jpg';
final uri = Uri.parse(urlImage);
final response = await http.get(uri);
final bytes = response.bodyBytes;
final temp = await getTemporaryDirectory();
final path = '${temp.path}/image-777.jpg';
File(path).writeAsBytesSync(bytes);
await Share.shareFiles(
[path],
// text: wording + ' ' + url,
// subject: 'Refer your friend',
);
provider_path.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="/" />
<external-files-path
name="external_files"
path="/" />
<cache-path
name="cache"
path="/" />
<external-cache-path
name="external_cache"
path="/" />
<files-path
name="files"
path="/" />
</paths>
I was searching whole day about this error and trying to understand FileProvider, but I have no idea what this error message tries to tell me, i already change '.' to '/' in provider_path.xml or '/cache/share' but still not working . If you want more info/code, write me in the comment.
I created a new app using capacitor 3, in this app I used the Filesystem to perform some functions. I created a service to handle everything related to the filesystem, but when I went to use the functions I had some problems.
When using Android 11 to create a directory using the mkdir() function I get the following error:
Unable to create directory, unknow reason.
Also on Android 11, when I try to create a file using only writeFile() it returns the following error:
FILE_NOTCREATED
For Android 10 and below the mkdir() function works correctly, but the writeFile() function causes the app to crash without any errors. Also, if I try to change the Diretory.ExternalStorage to Diretory.External I can create a directory in Android 11, but it still crashes when writing a file.
Using Android 11, I tried to write a txt file with a simple string and with also with a small base64 string. When using Diretory.External I can write the base64 string in a new file, but when using the Diretory.ExternalStorage I got the FILE_NOTCREATED error.
I did all the configurations and followed the steps in the documentation to set up AndroidManifest.xml.
Several tests have been done on emulators and phones with different versions of Android.
Tests with writeFile()
Android 11
base64 + Diretory.External = success
string + Diretory.External = crash
base64 + Diretory.ExternalStorage = error
string + ExternalStorage = error
Android 10 and below
base64 + Diretory.External = success
string + Diretory.External = crash
base64 + Diretory.ExternalStorage = success
string + ExternalStorage = crash
Android XML
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:requestLegacyExternalStorage="true"
>
<!-- Permissions -->
<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="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
MainFunction.ts
testeFilesystem(): void {
let path: string = 'test/project';
this.filesystem.checkAndCreateDir(path).then(_ => {
this.filesystem.writeFile(path, 'test.txt', 'test').then(_ => {
this.uicontroller.presentToast('file saved');
});
})
}
FilesystemService.ts
checkAndCreateDir(path: string): Promise<boolean> {
return Filesystem.readdir({
path: path,
directory: Directory.ExternalStorage
}).then(_ => {
return true;
}).catch(_ => {
return Filesystem.mkdir({
path: path,
directory: Directory.ExternalStorage,
recursive: true
}).then(_ => {
return true;
});
});
}
writeFile(path: string, filename: string, content: string): Promise<boolean> {
return Filesystem.writeFile({
path: path + filename,
data: content,
directory: Directory.ExternalStorage
}).then(result => {
if (result) {
return true;
} else {
return false;
}
});
}
Ionic Info
Ionic CLI : 6.16.1 C:\Users\User\AppData\Roaming\npm\node_modules#ionic\cli
Ionic Framework : #ionic/angular 5.6.7
#angular-devkit/build-angular : 0.1102.10
#angular-devkit/schematics : 11.2.13
#angular/cli : 11.2.13
#ionic/angular-toolkit : 3.1.1
Capacitor:
Capacitor CLI : 3.0.0
#capacitor/android : 3.0.0
#capacitor/core : 3.0.0
#capacitor/ios : not installed
Utility:
cordova-res : 0.15.3
native-run : 1.3.0
System:
NodeJS : v14.16.1 (C:\Program Files (x86)\nodejs\node.exe)
npm : 6.14.12
OS : Windows 10
Got the same error ('FILE_NOTCREATED') on Android 12 using the following code after some writes:
// Save to filesystem
const resp = await Filesystem.writeFile({
path: fileName,
data, // base64 pdf
directory: Directory.Documents,
});
The error occurs because of the same filename. After adding the current date with the following way, the error was solved:
const currentDate = new Date().toLocaleString().replace(/[,:\s\/]/g, '-');
const fileName = `myFile-${currentDate}.pdf`;
Be aware to replace all slashes because if not it will create sub-directories and if 'recursive: true' is not added the following error occurs on android:
Parent folder not found.
I found this issue on capacitor's github, maybe it can help you, in short it says:
If you target SDK 30, then android:requestLegacyExternalStorage="true" will not work on Android 11+ devices.
Google doesn't allow to programmatically access external shared files anymore. (Documents and ExternalStorage)
https://developer.android.com/training/data-storage/use-cases#opt-out-in-production-app
Error: Error: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{c896ebc 5117:org.nativescript.Jztong/u0a11
1} (pid=5117, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
Add this lines in your app/App_Resources/Android/AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
If you are using android v6 you should requires permission at runtime, you can use nativescript-permissions plugin.
var perm = permissions.requestPermission(android.Manifest.permission.WRITE_CONTACTS, "I need write Contact permission!");
perm.then(() => {
//addContact();
}).catch(() => {
alert("Oops, No permissions to write contact!");
});
I have created a scenario by creating a myScenario.sdl in my local config folder /atg/registry/data/scenarios/myScenario.sdl
myScenario.sdl
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE process SYSTEM "dynamosystemresource:/atg/dtds/pdl/pdl_1.0.dtd">
<process author="admin" creation-time="1413804041263" enabled="false" last-modified-by="admin" modification-time="1413804191188">
<segment migrate-subjects="true">
<segment-name>ItemAddedToOrder</segment-name>
<!--================================-->
<!--== Item added to order Quantity with fraction is defined -->
<!--================================-->
<event id="1">
<event-name>atg.commerce.order.ItemAddedToOrder</event-name>
<filter construct="event-property-filter" operator="isNotNull">
<event-property construct="event-property">
<property-name>quantityWithFraction</property-name>
</event-property>
</filter>
</event>
<!--================================-->
<!--== Log a message message: Quantity With Fraction is Defines logTriggeringEvent: true -->
<!--================================-->
<action id="2">
<action-name>Log a message</action-name>
<action-param name="message">
<constant>Quantity With Fraction is Defines</constant>
</action-param>
<action-param name="logTriggeringEvent">
<constant type="java.lang.Boolean">true</constant>
</action-param>
</action>
</segment>
</process>
And enabled the scenario:
Registry scenarioRegistry = scenarioManager.getScenarioRegistry();
byte[] data = (byte[]) scenarioRegistry.getItem(pScenarioPath);
String xml = null;
if (data != null) {
xml = new String(data, "UTF-8");
} else {
Assert.fail("No scenario is existed to enable/disable");
}
String updatedXml;
if (scenarioState && xml != null) {
updatedXml = xml.replaceAll("enabled=\"false\"", "enabled=\"true\"");
} else {
updatedXml = xml.replaceAll("enabled=\"true\"", "enabled=\"false\"");
}
scenarioRegistry.putItem(pScenarioPath, updatedXml.getBytes("UTF-8"));
Now with this above written code, I can both disable or enable the scenario by changing the state as false and true respectively. But I want to delete the scenario(please remember, my requirement is DELETE not DISABLE SCENARIO). I know using scenarioManager.updateScenario() deleted the scenario. Is my understanding right?
One more thing, I know I can delete the scenario directly from ACC. But I need to code via code not manually from ACC.
Please share your thoughts!
Did you try scenarioRegistry.removeItem(path);