In my application I receive a URL inserted by the user. This URL can be - for example - xx.sd. Using any web browser, this URL is a valid URL, but when try to open it by intent, a crash happens: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=xx.sd }.
I check this URL is valid URL by using this
Patterns.WEB_URL.matcher(model.getTarget().getUrl()).matches()
and open intent by using this code
Intent i = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(model.getTarget().getUrl()));
itemView.getContext().startActivity(i);
I know i can solve this issue by append http or https before URL if not exist but if my URL start with another protocol like ftp or file and other protocols. Can any one help me to handle this issue.
As you said this issue is really related to not well-formatted URL.
You can check for the ACTION_VIEW intent for the URL. First, this resolveActivity function check is there any application exists which can load URL. This will resolve the crash issue.
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}else{
//Page not found
}
}
OR, you can manage this by exception handling:
public void openWebPage(String url) {
try {
Uri webpage = Uri.parse(url);
Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request. Please install a web browser or check your URL.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Add un try-catch and call-again, such as:
public boolean startOpenWebPage(String url) {
boolean result = false;
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
try {
startActivity(intent);
result = true;
}catch (Exception e){
if (url.startsWith("http://")){
startOpenWebPage(url.replace("http://","https://"));
}
}
return result;
}
Related
I have a question about URL got from JSON details.
This is my code:
JSONObject currentEarthquake = couponCategoryArray.getJSONObject(i);
JSONObject properties = currentEarthquake.getJSONObject("campaign");
String name = properties.getString("name");
String promo_code = currentEarthquake.getString("promocode");
String goto_store = currentEarthquake.getString("goto_link");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
CouponCategory couponCategory = new CouponCategory(name, promo_code, goto_store);
couponcategory.add(couponCategory);
From JSON, I get 3 fields: name, promocode and goto_link.
goto_link is URL.
My intent is click the link on goto_link field to open the link in the browser.
I added an intent under goto_link String.
Some suggestion to code it correctly?
Add this.
Your URI starts with HTTP or HTTPS like this: http://www.google.com
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
startActivity(browserIntent);
public void openWebPage(String url) {
try {
Uri webpage = Uri.parse(url);
Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request. Please install a web browser or check your URL.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
How is you goto link formatted ?
anyways an answer from #MarkB solves it
string goto_link = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_link));
startActivity(browserIntent);
incase your link doesnt have the http:// part
string goto_link = "www.google.com";
if (!goto_link.startsWith("http://") && !goto_link.startsWith("https://"))
goto_link = "http://" + goto_link;
You can open a link in the browser like this:
yourButton.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(goto_store)
if (intent.resolveActivity(requireContext().packageManager) != null)
startActivity(intent)
}
I'm making a browser android application.
How do you determine if the url is a file? Like when you click a download button. Currently webView just loads and does nothing.
Do I need to check if the url ends with an extension and open it through intents? or is there another way?
You can do as following:
1) Check if the url is file:
if(URLUtil.isFileUrl(file)){
getExtention(url);
}
2) Get the extension:
public String getExtention(String url) {
String filenameArray[] = url.split("\\.");
String extension = filenameArray[filenameArray.length-1];
return extension;
}
Trigger intent according to the extension:
if(getExtention(url).equals("jpg")){
openGallery(url);
}
else if(getExtention(url).equals("pdf")){
openPDF(url);
}
3.a) OpenGallery() as ex:
public void openGallery(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
startActivity(intent);
}
3.b) Open Pdf
public void openPdf(String url){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
Hope this helps.
I'm trying to open a specific profile page from my android app, in the native facebook app. I've seen many answers about this topic, but none of them worked for me currently (maybe because some new versions)
Right now, I'm trying to open it like that:
public static Intent getFacebookIntent(PackageManager pm, String url ,String fbID) {
Intent intent;
try {
int versionCode = pm.getPackageInfo("com.facebook.katana", 0).versionCode;
boolean activated = pm.getApplicationInfo("com.facebook.katana", 0).enabled;
if(activated){
if ((versionCode >= 3002850)) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + url));
return intent;
} else {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + fbID));
return intent;
}
}else{
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return intent;
}
} catch (PackageManager.NameNotFoundException e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return intent;
}
}
where fbID its the user id, and url is the "link" from user graph request.
I've tried to change the url, to a specific url like
"https://www.facebook.com/some_user_name" but not luck.
the result of that code, is that the facebook app opens and crashes.
I do able to open it with webpage link.
Thanks
I want to open an Facebook link from my android application.
The URL is looks like http://www.facebbok.com/abcxyz. It should open the 'abcxyz' page in the Facebook application, but it is always opening in browser.
Code:
try
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activityContext.startActivity(browserIntent);
}
catch (ActivityNotFoundException ex)
{
ex.printStackTrace();
}
My android OS version is 6.0.1.
I have the same issue with Instagram, http://www.instagram.com/abcxyz, while other applications like Youtube work.
You should use facebook's custom url scheme to force app to open your page like below:
public Intent getFacebookIntent(String url) {
PackageManager pm = context.getPackageManager();
Uri uri = Uri.parse(url);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
}
catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
I just linked some social networks to my app as a preliminary test and using the same kind of code, the results I am getting are different for Facebook, Instagram and Twitter intents.
When I click Facebook or Twitter, it opens the app automatically when it's installed and uses browser when it's not. However, that's not the case with Instagram. The complete action using dialog pops up and that's not something I want to happen.
protected void LaunchInstagram() {
String InstagramUsername = "USERNAME";
String LaunchInstagram = "http://instagram.com/_u/" + InstagramUsername;
String InstagramURL = "https://instagram.com/" + InstagramUsername;
try {
this.getPackageManager().getPackageInfo("com.instagram.android", 0);
Uri Uri = Uri.parse(LaunchInstagram);
startActivity(new Intent(Intent.ACTION_VIEW, Uri));
} catch (PackageManager.NameNotFoundException InstagramAppNotFoundOpenBrowser) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(InstagramURL)));
}
}
You are using
String InstagramURL = "https://instagram.com/" + InstagramUsername;
So every Intent you'll try to launch, will open a chooser because this is a URL.
than every App that supports URL, like browsers and such, will try to handle that.
EDIT 1:
Try it this way:
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
I'm not sure if that will help you to open a specific user page on the App thou.
EDIT 2:
Try it this way:
Uri uri = Uri.parse("http://instagram.com/_u/USERNAME");
Intent insta = new Intent(Intent.ACTION_VIEW, uri);
insta.setPackage("com.instagram.android");
startActivity(insta);