Open file picker form my Android activity

与世无争的帅哥 提交于 2020-05-11 14:29:48

问题


I'm making an Android app which stores some downloaded pdf files inside the device's SD card. Everything works fine, but now I want to add a function to just pop up the default android file/folder browser showing the directory where my app stores all the PDF (with subdirectories in it) so that the user sees where his documents are stored and can easily browse them.

I've been throught many other SO questions and forum posts, but it seems this can only be done for music/images/contacts/etc. basically those file types which have a 'dedicated browsing system' but not with general file browsing.

I'm actually using this code:

File file = new File("/sdcard/MySorgenia/Documenti/");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
String type = "*/*";
intent.setDataAndType(data, type);
startActivity(intent);

But this will show me a "Choose the application to complete your action" dialog with many applications such as "Music" "Gallery" etc, but no general purpose one.

Thanks!


回答1:


Because In android there is no any native application which you can use as a File Explorer and responds to Intent type "*/*"

Implement your own File-Explorer Code for this purpose..

Look at these two Links..

  1. openintents

  2. Android-File-Explore




回答2:


public void loaddile()
   {
  private static final int gallery=12; 
  String type="*/*";

 Intent i=new Intent(Intent.ACTION_GET_CONTENT);
  i.setType(type);
 startActivityForResult(Intent.createChooser(i,"select file") ,gallery);
 }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == gallery && resultCode == RESULT_OK && data != null) {
        Uri uploadfileuri = data.getData();
        File file = new File(uploadfileuri.getPath());


    }
}



回答3:


Most android distributions do not come with a default file browser, and the behavior you noticed is the default android behavior. If there's any good third party file browser installed, it will automatically show up in that list. However it is not guaranteed that every end user will have a file browser installed. A general purpose fragment-widget can be created for this (and probably shared with others).




回答4:


Since Android 4.4 KitKat (API level 19), there is an Android built-in file picker: your app invokes the ACTION_OPEN_DOCUMENT and/or ACTION_CREATE_DOCUMENT intent and receives the files returned by document providers. More info about that can be found here:

Open files using storage access framework | Android Developers

Depending on where you want to store files, you may need to request permission:

Request App Permissions | Android Developers

Here is a how to:

An Android Storage Access Framework Example - Techtopia.

And a great working example is Ian Lake's Local Storage. Its source can be found on GitHub:

https://github.com/ianhanniballake/LocalStorage

And the app can be downloaded from Google Play:

https://play.google.com/store/apps/details?id=com.ianhanniballake.localstorage



来源:https://stackoverflow.com/questions/12194912/open-file-picker-form-my-android-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!