In my app, I try to call an intent for a result but it is showing an "Activity not found exception".
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
mayby the scanner is not installed? try this:
if (checkpackage(context, "com.google.zxing.client.android")) {
((Activity) c).startActivityForResult(new Intent("com.google.zxing.client.android.SCAN"), 0);
} else {
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
((Activity) c).startActivity(marketIntent);
Toast.makeText(c, "Es ist kein Barcodescanner installiert", Toast.LENGTH_SHORT).show();
}
public static boolean checkpackage(Context ctx, String PACKAGE_NAME) {
try {
PackageInfo info = ctx.getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
return info != null;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Related
I am trying to capture image in android phone, but before capturing i wish to show user by which app to proceed further. My goal is to give user the list of apps which can click photo, but currently only camera photo is showing. Please guide me resolving this issue.
I am trying to capture image from doc scanner and then upload it from my app.
public Intent getPickImageChooserIntent() {
Uri outputFileUri = getCaptureImageOutputUri();
List<Intent> allIntents = new ArrayList<>();
PackageManager packageManager = getPackageManager();
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent , 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
Intent mainIntent = allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new
Parcelable[allIntents.size()]));
return chooserIntent;
}
public Uri getCaptureImageOutputUri() {
Uri outputFileUri = null;
try {
File getImage = getExternalFilesDir("");
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "profile.png"));
}
} catch (Exception e) {
e.printStackTrace();
}
return outputFileUri;
}
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkCameraPermission()) {
startActivityForResult(getPickImageChooserIntent(), 1);
}
}
});
Calling code:
startActivityForResult(getPickImageChooserIntent(), 1);
I have an app in which i want to redirect user to app link in play store which is working fine, but if play store is not installed in device then i have to redirect user to app link in browser.How can I archive this issue.
code:-
private void startNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
Log.e(TAG,"Package name::"+packageName);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
and calling this method in onclick
startNewActivity(mContext, "market://details?id=com.example.something"));
private void startNewActivity(String packageName) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=packageName"));
startActivity(intent);
} catch (Exception e) { //google play app is not installed
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=packageName"));
startActivity(intent);
}
}
Or you can try like this.
Below mentioned check if app is installed or not.
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
Now check and use alternatives like this.
boolean installed = appInstalledOrNot("Your_Package_Name");
if(installed) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=packageName"));
startActivity(intent);
System.out.println("App is already installed on your phone");
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=packageName"));
startActivity(intent);
System.out.println("App is not currently installed on your phone");
}
Here is my function ... just copy and paste ... pass the package you need as parameter
public Intent getShareIntent(String applicationName, #Nullable Uri localImageUri, #Nullable Uri contentToShare, String textToShare) {
Intent intent = new Intent(Intent.ACTION_SEND);
boolean haveTheApp = false;
if(localImageUri != null){
intent.setType("image/*");
}else{
intent.setType("text/plain");
}
List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().contains(applicationName)) {
intent.setPackage(info.activityInfo.packageName);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
if(contentToShare != null){
intent.putExtra(Intent.EXTRA_TEXT, textToShare + ": " + contentToShare.toString());
}else{
intent.putExtra(Intent.EXTRA_TEXT, textToShare + " #App");
}
if(localImageUri != null){
intent.putExtra(Intent.EXTRA_STREAM, localImageUri);
}
haveTheApp = true;
break;
}
}
if(!haveTheApp){
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=" + applicationName));
}
return intent;
}
I start an application with this code:
Intent hasApp = getPackageManager().getLaunchIntentForPackage("appName.app");
Intent intent = new Intent(Intent.ACTION_VIEW);
dataId = response.getdataId();
intent.setData(Uri.parse("appName:?pid=" + dataId + "&action=returntoapp"));
startActivityForResult(intent, Globals.DATA_REQUEST_CODE);
And i close the opened application with this code:
setResult(Activity.RESULT_OK);
finish();
But when the first app called the onActivityResult() method I got the same requestCode, but the intent is null, and the resultCode is ACTIVITY_CANCELED.
I don't understand why.
Try this
String packageName = "com.example.app"
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (Exception e) {
return false;
}
}
I'm trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.
Here is my code
private void shareTwitter(){
try {
FileInputStream fis;
fis = getActivity().openFileInput("photo.jpg");
Bitmap shot = BitmapFactory.decodeStream(fis);
File file = new File(MapView.path, "snapshot.jpg");
if(file.exists()){
Log.i("FILE", "YES");
}else{
Log.i("FILE", "NO");
}
Uri uri = Uri.parse(file.getAbsolutePath());
//Uri uri = Uri.parse("android.resource://com.gobaby.app/drawable/back");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("/*");
intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
intent.putExtra(Intent.EXTRA_TEXT, "Thiws is a share message");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
} catch (final ActivityNotFoundException e) {
Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
At the moment there is no exception in my logcat my app just displays a toast saying image failed to load.
Please what an I doing wrong?
This is what you need
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
This might be helpful for somebody:
private void sendShareTwit() {
try {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
String filename = "twitter_image.jpg";
File imageFile = new File(Environment.getExternalStorageDirectory(), filename);
tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
tweetIntent.setType("image/jpeg");
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo ri : lract) {
if (ri.activityInfo.name.contains("twitter")) {
tweetIntent.setClassName(ri.activityInfo.packageName,
ri.activityInfo.name);
resolved = true;
break;
}
}
startActivity(resolved ?
tweetIntent :
Intent.createChooser(tweetIntent, "Choose one"));
} catch (final ActivityNotFoundException e) {
Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
}
}
Here is solution:
private fun shareOnTwitter() {
val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)
val intent = Intent(Intent.ACTION_SEND).apply {
type = "image/jpeg"
putExtra(Intent.EXTRA_STREAM, uriForFile)
}
startActivity(intent)
}
I have been struggling to send text from my app to Twitter.
The code below works to bring up a list of apps such as Bluetooth, Gmail, Facebook and Twitter, but when I select Twitter it doesn't prefill the text as I would have expected.
I know that there are issues around doing this with Facebook, but I must be doing something wrong for it to not be working with Twitter.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
startActivity(Intent.createChooser(intent, "Share Text"));
I'm using this snippet on my code:
private void shareTwitter(String message) {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo resolveInfo : resolvedInfoList) {
if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
resolved = true;
break;
}
}
if (resolved) {
startActivity(tweetIntent);
} else {
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
startActivity(i);
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
}
private String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 should always be supported", e);
return "";
}
}
Hope it helps.
you can simply open the URL with the text and Twitter App will do it. ;)
String url = "http://www.twitter.com/intent/tweet?url=YOURURL&text=YOURTEXT";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
and it will also open the browser to login at the tweet if twitter app is not found.
Try this, I used it and worked great
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text=...."));
startActivity(browserIntent);
First you have to check if the twitter app installed on the device or not then share the text on twitter:
try
{
// Check if the Twitter app is installed on the phone.
getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(intent);
}
catch (Exception e)
{
Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();
}
For sharing text and image on Twitter, more controlled version of code is below, you can add more methods for sharing with WhatsApp, Facebook ... This is for official App and does not open browser if app not exists.
public class IntentShareHelper {
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.twitter.android");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
For getting Uri from File, use below class:
public class UtilityFile {
public static #Nullable Uri getUriFromFile(Context context, #Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents