I have the following code:
/** Open a browser on the URL specified in the text box */
private void openBrowser() {
Uri uri = Uri.parse(urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
When I input "http://www.google.com" to the textbox, it works fine. However, when I try something like "www.google.com" it crashes with:
No Activity found to handle Intent {
act=android.intent.action.VIEW
dat=www.google.com }
Am I using Uri wrong? Is there a way to extract full address from it? Or am I supposed to write code that adds http manually? e.g, if not starts with http://, add http://.
Thanks!
Am I using Uri wrong?
No. However, www.google.com is not a valid uniform resource identifier.
Or am I supposed to write code that
adds http manually?
Yes.
Related
I am using an intent to open up a specific string literal(the website address) in an Android device's browser.
If I go with
String myIntentString = "url:www.google.com"
Uri myInputUri = Uri.parse(myIntentString);
Intent intent = new Intent(Intent.ACTION_VIEW, myInputUri);
startActivity(intent);
Then the app crashes.
However, if I change my string to "http://www.google.com" (the correct format), then it obviously works.
Yes, this is a school assignment and it is specified that the string MUST be "url:www.google.com"
If it helps, this is the first assignment where we need to turn in the Android Manifest file. Does this mean it has to do with that?
What am I missing?
I am developing an app that takes the input url from user (www.xyz.com) and then I am writing this code to hit the link in the browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(Url));
startActivity(browserIntent);
I saw that the http/https are not added automatically then I added it myself by doing Uri.parse("http://"+"Url). but is there any way to do this task automatically? Can anyone suggest a better solution for it because when I hit the links in my solution I am not sure the link I am hitting is https or http.
Uri.parse() will only convert the given String to uri. It doesn't know what type of url/uri is it (http or https). So yes, you have to add it yourself
Although you can set a static text in your EditText so that when user enters the text/url, the http or httpsis static and cannot be edited.
editText.setText("http://");
Selection.setSelection(editText.getText(), editText.getText().length());
Actually you can check first if the url that user enter starts with http/https or not, if yes, you can ignore, and if no, you can add "http://" by the code
String textFromUser = editText.getText().toString();
textFromUser = textFromUser.startsWith("http://") || textFromUser.startsWith("https://") ? textFromUser : "http://" + textFromUser;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(textFromUser));
startActivity(browserIntent);
I want to open a data: url containing a pdf in an activity in my android application. I have something like the following code:
String url = "data:application/pdf;base64,JVBERi0xLjIgDQol4uPP0w0KIA..."; // shortened for brevity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
I am seeing an error:
error opening uri: No Activity found to handle Intent {
act=android.intent.action.VIEW dat=data:application/pdf... (followed
by the rest of the data url).
How can I resolve this?
Since approximately zero apps in existence will support that scheme, you will need to decode the PDF yourself, write it to a file, and then open the PDF viewer on the file.
You need to have an app that can handle opening pdf documents. If you still wish to do this catch an ActivityNotFoundException and tell the user to download one.
I'm trying to open a url in a web browser using the following code,
does not work
Bundle ex=in.getExtras();
link=ex.getString("link");
public void onClick(View arg0) {
Intent t=new Intent(Intent.ACTION_VIEW);
Uri u=Uri.parse(link);
t.setData(u);
startActivity(t);
}
but i'm havinng a weird problem.When i use the exact url instead of string "link" everything works as it should, but when i go with the String link that i created i get the "no activity found to handle intent" exception.But i'm sure the link contains the right url, the same that i used before.
this works
public void onClick(View arg0) {
Intent t=new Intent(Intent.ACTION_VIEW);
Uri u=Uri.parse("http://google.com");
t.setData(u);
startActivity(t);
}
i'm sure that in the first case link contains the url (http://google.com), besides i printed on a textview to check!
Well, the problem was caused by a whitespace at the of 'link'.I used link.trim() and it worked correctly. Thank you guys for your help.
That is because Intent.ACTION_VIEW acts differently depending on the data you pass with setData.
So if you pass an Uri/URL it will open the browser, but if you pass a String (it doesn't matter that the string actually contains an url) it doesn't know what to do with it.
If you need to create the link from code you can use Uri.withAppendedPath
Hi
I want to write an app to call default browser and redirect to a designated url.
Any suggestion to 1)call the default browser, 2)redirect to a designated url.
Thanks
you just want to launch an ACTION_VIEW intent with the Uri of the webpage as your data element :
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("http://www.bbc.co.uk"));
startActivity(httpIntent);
To open the default browser use an Intent with the action VIEW. To tell the browser which page to load us the data-part of the Intent.
Example:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(browse);
Since this is a basic task in Android you might want to read some basics about Intents in Android.