samedi 23 mai 2015

Android open pdf file via Intent

I have some pdf files in some folder in sdcard. I created an app that shows all pdf as ListView. When i click on any pdf file it gives error in OfficeSuite application (UNSUPPORTED OR CORRUPT FILE FORMAT. Is something wrong with the code. Here is the code.

//Code for Items displayed as ListVIew

    ListView lv;
    ArrayList<String> FilesInFolder = GetFiles(Environment.getExternalStorageDirectory()
            + "/SOMEFOLDER");
    lv = (ListView) findViewById(R.id.filelist);

    lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, FilesInFolder));



    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // Clicking on items
            open_File();
        }
    });

    public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyReports = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i=0; i<files.length; i++)
            MyReports.add(files[i].getName());
    }

    return MyReports;
}

//Code for opening files VIA Intent

    public void open_File(){
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent1 = Intent.createChooser(intent, "Open With");
    try {
        startActivity(intent1);
    } catch (ActivityNotFoundException e) {
        // Instruct the user to install a PDF reader here, or something
    }

Error:

Corrupt or unsupported file format

Aucun commentaire:

Enregistrer un commentaire