2

I want to browse the sdcard for pdf files and then want open it in pdfviewer. Whatever I tried open the sdcard but doesn't open it in pdfviewer.

How can i do this?

public void onClick(View v) {

    Intent intent = new Intent();
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Pdf"), REQUEST_PICK_FILE );

}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
         if (resultCode == RESULT_OK) {
                switch (requestCode) {
                case REQUEST_PICK_FILE:
                {
                    Uri data = result.getData();

                    if(data.getLastPathSegment().endsWith("pdf")){
                        startActivity(new Intent(this, PDFReaderAct.class)); 
                    } else {
                        Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();   
                    }  }             
                }
            }
    }
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
madeeha ameer
  • 479
  • 2
  • 8
  • 22

4 Answers4

1

Try This :

File targetFile = new File(Environment.getExternalStorageDirectory(), "/Android/data/...../yourpdf.pdf");
Uri targetUri = Uri.fromFile(targetFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
this.startActivity(intent);
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
Arash GM
  • 10,316
  • 6
  • 58
  • 76
0

Try this code:

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);
Ricky Khatri
  • 952
  • 2
  • 16
  • 42
0

Try this code.

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent,1);
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
0

This worked for me. If you need you can uncomment the below few lines.

 public void browsePdf(View v) {

    Intent intent = new Intent();
        intent.setType("application/pdf");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1);

   }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1:
            {
                Uri data = result.getData();

             //   if(data.getLastPathSegment().endsWith("pdf")){
                    Intent intent_pdf=new Intent(this, PdfViewer.class);
                    intent_pdf.setData(data);
                    startActivity(intent_pdf);

              //  } else {
                //    Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();
              //  }
              }
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
sandesh hegde
  • 34
  • 1
  • 12
  • I don't really know if your answer works, but I think it would be better if you post less peace of code, showing only the essence of the change OP needs to make in his code. Your code doubles OP's code except for 2 rows, so it's not easy to see. Also, it's not necessary to mention upvoting in your answer, as it something obvious. Don't get me wrong, it's really great that you answered, but you can make you answer looking better, and people will upvote without reminding. – Alex Butenko Jan 03 '16 at 19:37
  • @AlexButenko ,Hi ,sorry new here , didn't know about that.What do you mean by 'OP'. He forgot to pass URI in the intent in his above code, Therefore ,its the same code but I am passing the URI along the intent which is used to get the path of the file . – sandesh hegde Jan 05 '16 at 11:48