How to display a dynamically genrated PDF in Android app - android

In my situation, in Activity A, when clicking a button A, it's supposed to open a pdf file. I can't place the pdf in our server and load it using something like this: https://docs.google.com/viewer?url=https://www.mysite.ca/.../file.pdf. Reason being this: 1) this pdf file is dynamically generated; 2) the path to this pdf is protected.
So I tried this: 1) first save the dynamically generated content (which is retrieved from a web service) in a file in phone; 2) load that saved file in the activity B opened when clicking button A in activity A.
Edited:
In Activity A:
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppUtils.preparePdfInPhoneAndStartActivity(this, serviceUrl, inputsMap);// the last 2 params are the web service url and inputs
}
});
In preparePdfInPhoneAndStartActivity():
{
...
//== decode the fileContent string - fileContent is a encoded byte string containing the pdf content
byte[] decodedFileContent = Base64.decode(fileContent, Base64.DEFAULT);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
fileNameInPhone = dir+"/"+fileNameInPhone;//fileName is the pdf file name, here contacted as the full path, it has this value: /storage/emulated/0/Download/file.pdf
OutputStream out = new FileOutputStream(fileNameInPhone);
out.write(decodedFileContent);
out.close();
File file = new File(fileNameInPhone);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);// activity is a instance of Activity A.
}
The pdf is opened in second activity (using Intent.ACTION_VIEW). But "Back" to activity A ending up button A frozen (not clickable). The other buttons in activity A still works.
Something I noticed while debugging:
1)the path of the saved pdf file is: /storage/emulated/0/Download/file.pdf. But I couldn't find this file at all in phone at this location: Computer\Galaxy s3(964)\Phone\Download. After unplug the phone and plug it again into my computer, this file shows.
2) log out the app and login again, "button A" is clickable again.
Because of 2) observation, I tried to add this line after launching the second activity:
activity.finish();
This fixes the frozen button A, but when backing from second activity, it doesn't back to the last screen/fragment in activity A, but app hidden in background and bring it up will land the first screen/fragment of activity A. I guess this is because of "activity.finish();"
3) without adding "activity.finish();", when backing to activity A, button A frozen, clicking another menu and selecting the menu that launching the pdf file, it still open the pdf in second activity, but again, the menu is not clickable. From this observation, it appears that the second activity by Intent.ACTION_VIEW is not working properly with a file in phone?
so my questions: 1) is there better way to display the dynamically generated pdf content in Android app without causing this frozen behavior? 2)If "activity.finish();" has to be added after launching second activity by Intent.ACTION_VIEW, how to restore/keep the status of activity A.
Thanks in advance!
Shawn

Related

How to find contents of a pdf when launched through button in andorid

I am new to Android.
I have developed a small app which launches pdf files from Asset when a button is pressed.
public class Finish_Center extends AppCompatActivity {
PDFView Sample;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Sample);
Sample = (PDFView) findViewById(R.id.Sample);
Sample.fromAsset("Book1.pdf").load();
This loads the pdf. But it does not enable me to search the content.
There is no search icon in the launched pdf. Hence I am not able to use "find" option of pdf.
As the pdf attached are big and used as reference I want to search content.
I tried keeping key pad visibility (To try (pressing Cntrl+F) but since there is no text view, Keypad is not visble.
Can someone suggest a method so that I will be able to search content in the pdf that is launched.
Regards,
Ashay
Use the method indicated below, so that each user may open it using their device's default app (which will probably provide a search feature).
File book = new File("path/for/your/Book1.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(book), "application/pdf");
startActivity(intent);

Open pdf always open document 1st page

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

After intent to show image not all functions appear

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.

open part of web page in android using uri intent

I have an android app that lists items with url links and on clicking opens the pages using uri,
as below
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
I would like to open only for example a video or image in that page but not the whole of it.
Is that possible in android?
I got what you want to ask.
NO, it is not possible with Intent to open a part of a webpage, it will open entire page,
I suggest you to
1. download and save the image to your drawable folder.
2. create a new actvity , and in the xml associated with that activity add a imageview
3. place your image in that imageview
4. and on the click of the link start the activity with the intent
I would suggest to either use an ImageView (load the image into it - Download image for imageview on Android) or a VideoView (as stream - http://code.tutsplus.com/tutorials/streaming-video-in-android-apps--cms-19888).
It's not possible with an intent.

Show GalleryView by intent with my own Activity is not visible

I have following requirement.
When the App receives a image file from server, it should open the image automatically in default image viewer.
Normal case:
Activity is visible.
Received image file from Server.
App send Intent.ACTION_VIEW
Gallery View shows the downloaded image.
Fail case:
Activity is not visible. (E.g. Press Home and return to launcher.)
Received image file from Server.
App send Intent.ACTION_VIEW
Nothing happens. (<-- Fail)
** If I go back to my App then I can see the Gallery View.
Is there anything I can do to get the Gallery View to show even my activity is not visible?
How i start the Gallery view:
final Intent openfileintent = new Intent();
openfileintent.setAction(android.content.Intent.ACTION_VIEW);
final File file = sharedfile.getFileInstance();
openfileintent.setDataAndType(Uri.fromFile(file), sharedfile.getMimeType());
startActivity(openfileintent);
Have found answer after trial and error.
There is 2 flags to add.
And most importantly, should use application context instead of activity context.
final Intent openfileintent = new Intent();
openfileintent.setAction(android.content.Intent.ACTION_VIEW);
(*1) openfileintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
(*2) openfileintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
final File file = sharedfile.getFileInstance();
openfileintent.setDataAndType(Uri.fromFile(file), sharedfile.getMimeType());
(*3) getApplicationContext().startActivity(openfileintent);

Categories

Resources