I have a button that opens a local pdf manual like this
File file = storeFile(is, "user_manual.pdf");
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This opens the document in Adobe Acrobat Reader that is installed and our setup guarantees that Acrobat Reader is always installed.
The problem is that the manual has 50 pages. If the user were to scroll down and hit back and select the user manual button, it takes us back to middle of the pdf which would be normally good but I have a specific requirement that it has to open the first page that has an index.
How do I open the document such that it always shows the first page of the pdf?
I have tried
intent.putExtra("page", 1); // parameter used on desktop Acrobat reader
and
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
but it always opens the pdf the second time where the user left the first time.
Try to close the view on your opened file .pdf, when the user press the back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode== KeyEvent.KEYCODE_BACK)
{
Intent intent = new Intent(PDFVIEW.this, MainActivity.class);
startActivity(intent);
finish();
}
return true;
}
I have two answers. If the first answer works, that would be great. If not, the second answer will certainly work with Adobe Reader, but it's more work.
Answer 1: Use an open parameter
Adobe has published a document entitled Parameters for Opening PDF Files and there's indeed a parameter named page. You use it like this:
intent.putExtra("page", 1);
Have you tried this:
intent.putExtra("page", "1");
Maybe the parameter needs to be passed as a string (although it's actually an integer).
Answer 2: introduce an open action
An open action allows you to trigger an action as soon as the document opens, e.g. you can execute some JavaScript the moment someone opens your document. In your case, you want to open the document on page one.
Adding an open action to a PDF document requires you to change the PDF document. You need some software that updates your PDF with this action. See How to set initial view properties? for a C# example. There are some Java examples here: iText 5 example / iText 7 example.
Mmmh,
I think you could use the AR command line options like:
(path to Adobe Reader) /A "page=100" "(Path To PDF file)"
or the AR activeX/ browser control:
"PDF browser controls are available through the AxAcroPDFLib.AxAcroPDF interface, which provides the following methods used to programmatically control the PDF document window:
●GoBackwardStack
●GoForwardStack
●GotoFirstPage
●GotoLastPage
....."
HTH, Reinhard
Related
for some time, I have been observing that modern android apps(Telegram) use a new way to open urls in the app. By default, the apps I have made, use ACTION_VIEW and the external browser open the url. These apps now manage that intent with a kind of in-app browser witch adapts to the same style. Any idea of what its?
sample
Those are Custom chrome tabs.In app browser.instead of using webview to open your Url, you are going to customize your chrome browser,to look alike your app.you can modify the toolbar tab color as per your app theme .You could give enter and exit animation like fragments transition.So the user wouldn't feel a big transition from your app to browser.For implementation details check this link https://developer.chrome.com/multidevice/android/customtabs
For security reasons also it could be useful,as because you are not going handle those url on you own or inside your app(there might be some security issues with JavaScript)
This is done with WebView. You will find the documentation very interesting https://developer.android.com/reference/android/webkit/WebView
This is achieved by WebView in an Activity that displays web pages. Instead of
Uri uri = Uri.parse("https://www.google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
You will load the page by calling:
webviewObj.loadUrl("https://www.google.com/");
You can check the official document which describes the whole process. Basic Usage of WebView has been described in this blog which is easy to comprehend and implement.
do the following:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
try this it helps you.
DONT mark this as duplicate, before reading what I need.
I have seen many similar topics, but in none of them I've found solution.
I need the simplest thing: In my application I have button "View Media Files". After clicking that button, i need to be opened (with built-in File Explorer) this directory - SD_CARD/my_folder where are media files (and I want to click any of them and they should be opened in default Media player)..
I have used all suggested answers on SO , like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("/sdcard/Recorder_Videos");
intent.setDataAndType(mydir, "*/*");
startActivity(intent);
but all they do: after clicking button, it opens "Choose File" menu:
(Where I cant still play media files when clicking)
The solution (not complete) I have found, was that I was missing file:// prefix. Here is my solution (however, it shows all kinds of applications on first view):
public void openFolder(String location)
{
// location = "/sdcard/my_folder";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file://"+location);
intent.setDataAndType(mydir,"application/*"); // or use */*
startActivity(intent);
}
p.s. Strange and surprising, but there doesnt exist the standard definition of "File Browser" in stock Android systems (unless you install 3rd party "File Explorer")..
That's why "resource/folder" mime-type doesnt work by default..
However, let's say a simple truth. File-Browser is a SIMPLE and ESSENTIAL part of any OS system. And it's quite disrespectful from Android, saying that it's not their job, and throwing the responsiblity to 3rd party apps.
You can use type DocumentsContract.Document.MIME_TYPE_DIR which works on several devices and launches File Explorer. You can refer this SO for more details.
I'm going to develop a simple app to list .jpg files and after a click call an Intent.ACTION_VIEW, below the code:
File imageFile = new File(filename);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(imageFile), "image/jpeg");
startActivity(i);
The intent works properly, in fact in app I receive the message to select an app to show the selected image. But I noticed that after the choice, for example with Google Photos, not all function are enabled. For example the share function is not visible, has someone already noticed this behavior?
Some other details; if I choose Google Photos I only have the "Info" and "Guide and Feedback" options. If I choose gallery app (I have a Samsung Ace 3) I have only "Info" and "Set as wallpaper" options.
Note: if I open the same image directly from Google Photos all functions are enabled...
The ACTION_VIEW intent calls another application to view the content that you present, in your case a JPEG image. If there is more than on application that can handle that Intent, Android lets the user choose an app to display the image. Once the application receives the Intent, it is up to that application to display the content how it sees fit.
Basically, you have no control over what functions are available with Intent.ACTION_VIEW. If you want more control, you can create your own Activity in which you can view the image and provide whatever functions you would like.
I have a number of urls that points to pdf files. I would like to view these in my application using the third party application. I have "Drive PDF Viewer". I am using the code below. When I press on a button on my app, that has a setOnCLickListener, the pdf file is downloaded, and instead of allowing me to choose the third party application that i would like to use to open my downloaded pdf, a web page is presented (yahoo website). However, if i go to my notification drawer and click on the downloaded file, only then does the option to select "Drive PDF Viewer" is presented. My question is, how do i go straight to choosing which third party pdf viewer i would like to use without having this webpage presented or having to access the notification drawer? I have no idea where this webpage comes from.
pdfButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
/*Display pdf here*/
Log.e("ArticleFragment", "pdf Button Pressed! url: " + pdfUrl);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl));
startActivity(browserIntent);
}
});
You should download the remote file yourself, storing it in a local cache directory, then use the same code but with the local URI.
hi is there a way to ask the os to open a file with its predefined programm?
I do not know what kind of file it might be, maybe a photo , txt anything.
I want to issue a command to the os to ask it to open the file(i have the filepath) with any program the os wants. It might be the case that the os opens the dialog asking the user to select one program to open the file(that is fine with me).
thanks
You do that via an intent chooser:
please refer to : http://developer.android.com/training/sharing/send.html
This is done through an Intent You will need to set the action and the data.
//from inside an Activity
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.fromFile(theFileYouMentioned));
try{
startActivity(i);
}
catch(ActivityNotFoundException) {
//user doesn't have an app that can open this type of file
}