How do I fix "startActivity() in ContextCompat cannot be applied to?" - java

I am using Google Vision OCR to grab the email from a business card (the OCR Graphic activity) and send it to the the To destination in the SendEmail activity. My log shows that the email text is detected.
I tried to set the intent to send it to the next activity, but I am getting two errors, "cannot resolve constructor Intent" on my new intent, and start activity cannot be applied to.
This is the OcrGraphic activity
List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
float left = translateX(elements.getBoundingBox().left);
float bottom = translateY(elements.getBoundingBox().bottom);
if (elements != null && elements.getValue() != null) {
if (elements.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*#\"\n" +
"\t\t+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$") || elements.getValue().contains("#")) {
Log.e("elementsemail", elements.getValue());
String email;
email = elements.getValue();
cEmail = email;
Intent sendIntent = new Intent(this, SendEmail.class);
sendIntent.putExtra(Intent.EXTRA_EMAIL, cEmail);
startActivity(sendIntent);
}
this is my Send Email activity
private void sendMail(){
Intent getIntent = getIntent();
String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);;
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
I want to send the email address to the SendEmail activity. I am new to java and android, any help is welcomed.

I think you're problem is how you obtain the extra(EXTRA_EMAIL)
Replace String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);; with String recipientList = getIntent.getStringExtra(Intent.EXTRA_EMAIL);

please replace this :
Intent sendIntent = new Intent(this, SendEmail.class);
whit this :
Intent sendIntent = new Intent(getApplicationContext(), SendEmail.class);
edit
You need to pass context in a constructor like this
private Context context;
OcrGraphic(GraphicOverlay overlay, TextBlock text, Context context) {
super(overlay);
this.context = context;
}
And then
Intent sendIntent = new Intent(context, SendEmail.class);

Related

Problem sending data to other activity in Android Studio

I want to send data from three activities to the last activity, how can I do
1. First Activity
else {
//first activity want transfer data last activity
Intent intent = new Intent(Register.this, Register2.class);
intent.putExtra("name", reg_name);
intent.putExtra("surname", reg_surname);
intent.putExtra("email", reg_email);
startActivity(intent);
}
2. Second Activity
else {
//second activity transfer data last activity
Intent intent = new Intent(Register2.this, PhoneNumberOtp.class);
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
}
3. Third Activity
else {
//third activity transfer data last activity
Intent intent = new Intent(PhoneNumberOtp.this, OTPVerification.class);
intent.putExtra("phoneNumber", reg_phone_number);
startActivity(intent);
}
Last Activity
else {
Intent intent = new Intent(OTPVerification.this, HomeActivity.class);
//get data register activity
String name = getIntent().getExtras().getString("name");
String surname = getIntent().getExtras().getString("surname");
String email = getIntent().getExtras().getString("email");
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");
Extras you put on an intent are not automatically passed along to subsequent activities after the first one they are sent to directly. If you have some data you want to pass through multiple activities, you have to re-add it to the new sending intent for each step. For example:
First
Intent intent = new Intent(UserActivity.this, AccountActivity.class);
intent.putExtra("name", reg_name);
intent.putExtra("email", reg_email);
startActivity(intent);
Second
Intent intent = new Intent(AccountActivity.this, RegisterActivity.class);
// get the received data and add it to the new intent
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
intent.putExtra("name", name);
String email = recv.getStringExtra("email");
intent.putExtra("email", email);
}
// then add any new data
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
Third
// RegisterActivity now has 4 strings available
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
String email = recv.getStringExtra("email");
String username = recv.getStringExtra("username");
String password = recv.getStringExtra("password");
}
In the example you posted, the last activity would only have access to phoneNumber, since that's all you passed it from the third activity.

send Email through Android Intent

I have tried to send Email through Android Intent by using below code
Intent sendIntent = new Intent();
sendIntent.putExtra(Intent.EXTRA_TEXT, EmailContent);
sendIntent.putExtra(Intent.EXTRA_EMAIL, RecipientName );
sendIntent.putExtra(Intent.EXTRA_SUBJECT , subject );
sendIntent.setType("message/rfc822");
sendIntent.setAction(Intent.ACTION_SEND);
Intent chooser = Intent.createChooser(sendIntent , chooser_title );
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
In the email app the recipient details are not getting update whereas all the other details such as subject, body is getting updated with my input . Could you please suggest what needs to be done to resolve this .
Try to pass the EXTRA_EMAIL as string array.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Try using this:-
Create String resource for recipients:-
<string-array name="receipients">
<item>mgf#kgf.co</item>
<item>sdf#kgf.co</item>
</string-array>
and use this intent
Intent gmailIntent = new Intent(Intent.ACTION_SEND);
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, resources.getStringArray(R.array.receipients));
gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EmailContent);
gmailIntent.setType("message/rfc822");
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
final PackageManager pm = context.getApplicationContext().getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") ||
info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
if (best != null)
gmailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
try {
startActivity(gmailIntent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context.getApplicationContext(), getString(R.string.you_do_not_have_gmail_installed), Toast.LENGTH_SHORT).show();
}

getArguments() in Activity Class?

I'm still learning AS & java and recently bought an app code but it has "getArguments()" inside of fragment which I'd like to convert it into activity. Help is appreciated.
This is the code :
String weburl = getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
String data = getArguments().containsKey(LOAD_DATA) ? getArguments().getString(LOAD_DATA) : null;
if (data != null) {
browser.loadDataWithBaseURL(weburl, data, "text/html", "UTF-8", "");
} else {
browser.loadUrl(weburl);
}
How do I write the same code in an activity?
I think what you are looking for is how to send info to an Activity, for that you need Intents
Please verify this info: https://developer.android.com/reference/android/content/Intent
Basically:
public static final String EXTRA_MESSAGE = "extra message";
...
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello this is an intent";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
To retrieve that data you have to do:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
I invite you to check this out: https://developer.android.com/training/basics/firstapp/starting-activity#java
Intent extras are the activity equivalent of fragment arguments.

how to share a File object by email?

I create a File object programmatically. I wonder how I can share it by email directly without saving it on the device.
I would like to have a method like this:
private void sendFileByEmail(File aFile, String emailTitle)
{
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, emailTitle);
// and then ???...
}
Thanks for your help !
This could be and option
private void sendFileByEmail(File aFile, String emailTitle)
{
Uri path = Uri.fromFile(afile);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}

Restricting share options

Is there a way to restrict share options in an Android app? I have tried using the ShareActionProvider or just simply starting an intent using the Intent.ACTION_SEND intent option. Basically I want to be able to restrict sharing through email only or something of the sort.
you can use something like this, but instead of facebook look for a different name
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"this is a string");
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri); //Share the image on Facebook
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
break;
}
}
You can customize the intent chooser as per your need like this -
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject to be shared");
if (StringUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://link-to-be-shared.com");
}else{
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text message to shared");
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Hope this helps you.

Categories