0

I have the following PHP code from an HTML application (user is using Chrome browser on Android)

$path_full = '../files/this_is_my_dir/test.pdf';
$path_exploded = explode('/', $path_full);
$filename = end($path_exploded);

if (file_exists($path_full)) {
    $finfo  = new finfo(FILEINFO_MIME);

    header('Content-Description: File Transfer');
    header('Content-Type: ' . $finfo->file($path_full));
    header("Content-Disposition:attachment;filename='" . $filename . "'");
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path_full));
    readfile($path_full);
}

When a user clicks an icon it will force download (with above code) the file (which is outside public root) to the android download folder. But we want that the PDF automatically after download opens (just like click on a a href link). Is this possible ?

PostMans
  • 338
  • 5
  • 18

1 Answers1

0

Open an intent:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

This is the source

Community
  • 1
  • 1
Javi Mollá
  • 786
  • 7
  • 18