1

I am trying to save a PDF file to an external Storage Directory. I think I am missing something obvious, so perhaps we can solve this together.

First I click my save button and check the directory exists.

File direct = new File(Environment.getExternalStorageDirectory() + "/CompanyName");

//if directory does not exist then create it
if (!direct.exists()) {
    filedir.mkdirs();
} 

Then I need to save the PDF File. The way my app is setup I have the PDF already downloaded into my PDF viewer so I can access the file directly. I think I am missing a key concept here, but most online answers are for images or creating text files.

pdfFile = new File(direct, imageTitle.getText().toString());

try {
    //try to create a new file
    pdfFile.createNewFile();

} catch (IOException e) {
     e.printStackTrace();
}
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50
  • Your second code snippet does not write anything to the `FileOutputStream`. And, if you already have the file (`displayedFile`), then what are you really trying to do here? Are you trying to copy a file from one place to another? – CommonsWare Apr 28 '19 at 23:14
  • I know, I've been working on this for a while and I think deleted the useful stuff I used to have, been working on other angles. I download the file from a url so I can properly use a PDF viewing library. From there the user can choose to download the file to their phone's storage. Edit: I think I had a write() method from out earlier – C. Skjerdal Apr 28 '19 at 23:17
  • "From there the user can choose to download the file to their phone's storage" -- OK... but if the file is already downloaded, then, isn't this just copying a file? – CommonsWare Apr 28 '19 at 23:18
  • Looking now I guess I already do a download from the url and I save it to internal storage. (This is for an offline backup cache). I guess I want to keep the saved file private, and then additionally allow the user to save the file to an external folder. The private folder keeps people from accessing user specific files. I'll update some code in my question to something closer to a solution. – C. Skjerdal Apr 28 '19 at 23:29
  • I just did an update to clean up my question. As far as your comment about copying, is their an easier way to copy my internally saved file to the external directory? – C. Skjerdal Apr 28 '19 at 23:32
  • 1
    You have already downloaded the file and it is probably in internal storage, so all you need to do is copy that file to another file in external storage. Make sure to have the proper permission already – dglozano Apr 28 '19 at 23:34

0 Answers0