0

I have PDF files in my assets folder now i am able to view that PDF files in list view .But the problem is now on click of any pdf i want to open pdf in my pdf viewer. Here is my code

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager asset = getAssets();
        try {
            final String[] arrdata = asset.list("PDFfolder");
            List<String> pdflist = new ArrayList<String>();
            int size = arrdata.length;
            for(int i = 0;i<size;i++)
            {
              if(arrdata[i].contains(".pdf"))

              {
                pdflist.add(arrdata[i]); 
               }
            }
            ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pdflist);
            ListView listView = (ListView) findViewById(R.id.listView1);
            listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                if(position == 0 ) {
                    File pdffile = new File("file:///android_assets/AAI.pdf");
                    //File ff = new File(getAssets().open("AAI.pdf"));
                     Uri path = Uri.fromFile(pdffile);
                     Intent intent = new Intent(Intent.ACTION_VIEW);
                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     intent.setDataAndType(path, "application/pdf");
                     startActivity(intent);    
                     }

                }

        });
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     }
}

Now please help me out how to open it using intent from my assets folder. i am getting error of having no activity found to handle intent as i already have pdfviewer in my phone.

user3387601
  • 59
  • 3
  • 6

2 Answers2

0

You need an app installed to handle opening the PDF. You should use the Intent Chooser, like so:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
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
}   

The files in the assets directory doesn't get unpacked. Instead they are read directly from the APK ZIP file.

So, you really can't make stuff that expects a file accept an asset 'file'.

Instead you'll have to extract the asset and write it to a separate file ,like this :

File f = new File(getCacheDir()+"/fileName.pdf");
  if (!f.exists()) try {

    InputStream is = getAssets().open("fileName.pdf");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();


    FileOutputStream fos = new FileOutputStream(f);
    fos.write(buffer);
    fos.close();
  } catch (Exception e) { throw new RuntimeException(e); }

Also you can use the AssetManager to manage files in your asset folder. In the activity, call getAssets () method to get an AssetManager Instance.

Hope it helps you.

Shruti
  • 1
  • 13
  • 55
  • 95
  • Is it not possible without extracting....Because i have read it here http://stackoverflow.com/questions/13517412/how-to-open-a-pdf-file-from-res-raw-folder Here also they haven't given solution can you please look at my code and tell me what i will i have to do?Any help wud be appreciated – user3387601 Apr 11 '14 at 12:31
  • Thanks @Shruti ...But i have pdf names in list view i have to open different pdfs on click of different names.... – user3387601 Apr 19 '14 at 06:06
0

Work perfectly in my project try this code

/**
         * get on item click listener
         */
        userList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    final int position, long id) {

                TextView tv_paper_name = (TextView) v.findViewById(R.id.tv_paper_name);
                String PaperName = tv_paper_name.getText().toString();
                File extStore = Environment.getExternalStorageDirectory();
                File myFile = new File(extStore.getAbsolutePath() + "/Exam Papers/"+PaperName+".pdf");

                if (myFile.exists()) {

                File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Exam Papers/" +PaperName+".pdf");  // -> filename
                Uri path = Uri.fromFile(pdfFile);
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    try {
                    startActivity(pdfIntent);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this, "Download file first", Toast.LENGTH_SHORT).show();
                }


            }
        });
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67