Open PDF in browser - android

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.

Related

Show files on default viewers without downloading from URL

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")

display pdf within google drive

I found simple solution if anyone ask solution use these it is not necessary to use webview.
String fullPath = "https://drive.google.com/open?id=1svKjgGhtS90u7n4D64ZcnzGxh9-X4kJx";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
I found simple solution if anyone ask solution use these it is not necessery to use webview.
String fullPath = "https://drive.google.com/open?id=1svKjgGhtS90u7n4D64ZcnzGxh9-X4kJx";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);

Android web browser, check if url is a file

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.

How to use Uri.parse in android

Hi I'm testing on encryption & decryption of a web url & trying to develop intent to web browser.
String decrypted = DecodeAES.decrypt(AESkey, encrypted);
decryptedValue.setText(decrypted);
here i want to retrieve "decryptedValue" and to be placed in the Uri.parse
actually here the "decryptedValue" contains some website to be opened in webbrowser.
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Please tell me how to use Uri.parse & what to be used.
Thanks in advance.
Change -
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.google.com"));
to -
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

Open a URL in Android's web browser from application

How can I open a url in the android web browser from my application on clicking a button.
I tried this :
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
but I am getting exception:
but I got an Exception :
"No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"
You are doing it fundamentally correct, you just need to include the full url.
String strURL="http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https, if you want a secure connection over SSL.
Almost seems like this would be useful to read... from the page:
Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);
There is a second way, involving the WebView, but that seems like it's not your question. Hope this helped.
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Try this:
String strURL = "http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;

Categories

Resources