2

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

1 Answers1

3

I think you forgot to specify the file. See your code, you only pointed it to the folder, but no the file itself. I think that is why it tells you that it is in wrong format, because it does not end with .pdf

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "SOMEFOLDER" + File.separator + "pdffile.pdf");

EDIT: Modify methods according to your comment

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

public void open_File(String filename){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER", filename);
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
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • SOMEFOLDER has many pdf. I created a ListView in which all those pdf are visible. I need to open that particular pdf by clicking on it. So in that case how could I specify a specific file. – Rohit Kumar Verma May 23 '15 at 08:46
  • Specifying a particular filename will cause the same file to open again and again. But in my case i need to open that file which I clicked – Rohit Kumar Verma May 23 '15 at 08:50
  • Yes, I have modified my answer. You need to modify the methods a bit, you need to get the name of the file you clicked an pass it to the open file function. See my edit – Bojan Kseneman May 23 '15 at 08:52