2

Trying to open pdf file from download folder on device, i save the name of file in firebase and now i want to check if the file is already found on device and then open it else download from firebase.

File file = new File(Environment.getExternalStorageDirectory() + "filename");
System.out.println(Environment.getExternalStorageDirectory());

///but when i print Environment.getExternalStorageDirectory() i get 
///storage/emulated/0

if (file.exists()) {
    System.out.println(file.getAbsoluteFile());
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");
    target = Intent.createChooser(target, "Open File");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent = Intent.createChooser(target, "Open File");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
} else {
    System.out.println("file not found");
}
pa1.Shetty
  • 401
  • 3
  • 16
koby
  • 29
  • 6

1 Answers1

2

try this to get your file from download directory

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + filename);
Priyanka
  • 3,369
  • 1
  • 10
  • 33
  • i did what you suggets but now i get another exeption FileUriExposedException – koby Apr 30 '19 at 11:37
  • see this link https://medium.com/@ali.muzaffar/what-is-android-os-fileuriexposedexception-and-what-you-can-do-about-it-70b9eb17c6d0 – Priyanka Apr 30 '19 at 12:04