tanasi
tanasi

Reputation: 1804

Open folder on button click with "My files" application

I am making android application. On button click I just need to open specific folder where I have pdf's so user can choose some pdf to read from that folder.

I manage to list all files from pdf folder, but that is not what I need. Most logical solution is just to open folder with "My files" app which is already part of Android OS. Is there any whey to do this on android?

Upvotes: 1

Views: 3499

Answers (3)

tanasi
tanasi

Reputation: 1804

This code is working without "My Files" app which, is better solution.

    public class AndroidListFilesActivity extends ListActivity {

    File root;
    File pdf;

    private List<String> fileList = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        root = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        // ListDir(root);

        pdf = new File(root, "PDF");
        ListDir(pdf);
    }

    void ListDir(File f) {
        File[] files = f.listFiles();
        fileList.clear();
        for (File file : files) {
            fileList.add(file.getPath());
        }

        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, fileList);

        setListAdapter(directoryList);

    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        //selection.setText(fileList.indexOf(simple_list_item_1));
        OpenPdf(fileList.get(position).toString());
    }

    public void OpenPdf(String path)
    {
          File file = new File(path);
          if (file.exists()) {
              Uri p = Uri.fromFile(file);
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setDataAndType(p, "application/pdf");
              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

              try {
                  startActivity(intent);
              } 
              catch (ActivityNotFoundException e){
              }
          }
    }
}

Upvotes: 0

tanasi
tanasi

Reputation: 1804

Here is my code for listing files, how to open pdf files now on click.

    public class AndroidListFilesActivity extends ListActivity {

    private List<String> fileList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        File root = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath());    
        File pdf = new File(root, "PDF");
        ListDir(pdf);
    }

    void ListDir(File f) {
        File[] files = f.listFiles();
        fileList.clear();
        for (File file : files) {
            fileList.add(file.getPath());
        }

        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, fileList);
        setListAdapter(directoryList);
    }

}

Upvotes: 1

Aerilys
Aerilys

Reputation: 1639

Be careful. Not all Android devices have a "My files" app. So the best way is to create your own listing/file explorer.

Upvotes: 2

Related Questions