I have written following code to open url in browser but this url ends with pdf which I want to open in browser but i'm not getting browser option in file chooser.
startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse(httpUrl),"Choose"));
Try the following code to open a pdf file from the site in the browser.
public void button (View view) {
goToUrl ( "http://site address/director/test.pdf");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
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)
}
What I want to achieve is to display PDFs directly in browser.
private void openPDF(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
With this method the PDF is downloaded, but I have to manually open it in order to see it.
I remember this method worked just fine a while a go, did something change in the intent or chrome maybe?
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
If this can help.
I am having webservice which has a String URL response, URL could be of .PNG and .PDF I don't want to download image and pdf files from that url I want to show them on default viewer i.e. if i click to view image button then it should open image url to any default photo viewer and if i click to view pdf button then it should open pdf in default pdf viewer.
Please help Thanks in advance.
File file = new File(url);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String extension = file.getName().substring(file.getName().indexOf(".") + 1).toLowerCase();
String mimeType = myMime.getMimeTypeFromExtension(extension);
newIntent.setDataAndType(Uri.fromFile(file), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MasterPlan_Activity.this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
The basic concept here is opening an app that can handle your resource (JPEG or PDF). So, you need to request an Implicit Intent with the data URI that needs to be shown.
You can use intent with specific filter to achieve that, as shown below
Image:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
Pdf:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
or
private static final String googleDocsUrl = "http://docs.google.com/viewer?url=";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl + url), "text/html")
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;
}
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.