0

I can not read a pdf file with my program. The code is simple but, unfortunately it does not work. thank you in advance

    String path =getActivity().getFilesDir()+"/test.pdf";
    File file = new File(path);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file),"application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent=Intent.createChooser(target,"Open File");
    startActivity(intent);
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
  • Use MuPdf for android https://mupdf.com/docs/how-to-build-mupdf-for-android – Vishal Raj Jan 13 '17 at 06:04
  • what kind of exception are you getting – AurA Jan 13 '17 at 06:30
  • is it possible that you don't have an app to open this pdf file? Without an error message there could be a lot of reasons. – Stefan Lindner Jan 13 '17 at 07:01
  • Thanks to everyone but I found the solition. The problem is that I use the version of android 6.0 +. In this version it is necessary to manually activate the permission to have access of recording by the program. Ex: settings-> app-> yourapp-> permissions. Thank you again to all the developer who gave their opinion. – Dos Argenté Jan 14 '17 at 20:18

3 Answers3

0

Here is code on how to open that file (test.pdf) with an Intent chooser:

File file = new File(getActivity().getFilesDir()+"/test.pdf");
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
}

Source

Community
  • 1
  • 1
Faraz
  • 643
  • 4
  • 15
0

PdfRenderer support is added in Android 5.0

Below Android 5.0, you have to make a workaround

See this blog written by CommonsWare for viewing PDF

N J
  • 27,217
  • 13
  • 76
  • 96
0

how to open pdf file click here for complete code

 private void openRenderer(String filePath) {
            File file = new File(filePath);
            try {
                mFileDescriptor = ParcelFileDescriptor.open(file,
                        ParcelFileDescriptor.MODE_READ_ONLY);
                mPdfRenderer = new PdfRenderer(mFileDescriptor);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75