0

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

Shawn
  • 530
  • 1
  • 5
  • 15
  • "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" -- [you did not index the file in the `MediaStore`](https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl). "is there better way to display the dynamically generated pdf content in Android app without causing this frozen behavior?" -- you do not show anything related to this button in your question; that will be difficult for anyone to answer. – CommonsWare Jul 10 '17 at 18:46
  • Thanks for your comments. for the first part, after saving the file in phone, if I call the scanFile() method in the other post, it will show up right away in computer? Btw, the parameter: String mimeType will be "application/pdf"? For the second part, nothing fancy at all: in the onclickListener of the Button A, I retrieve the content from a web service call and save it to a file in phone at this path: /storage/emulated/0/Download/file.pdf, then launch the Intent.ACTION_VIEW as described in my question. – Shawn Jul 10 '17 at 21:38
  • "nothing fancy at all" -- but since your problems are there, wouldn't it make sense to actually have that code in your question? How do you expect people to help you with code that they cannot see? – CommonsWare Jul 10 '17 at 21:46
  • Thanks. I added the code segment, hoping that's what you're asking. – Shawn Jul 11 '17 at 14:13

0 Answers0