Android File Picker Example

Do you need to show file picker in your Android application to allow users choosing file in their local storage? This tutorial shows you how to show a simple file picker and get the File object of selected file.

Layout

First, create the XML layout. To keep this tutorial simple, there is only a Button that will trigger to show the file picker when it's clicked and a TextView for showing the path of selected file.

choose_file.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/fragment_item_edit_content"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="20dp" >

      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" >

          <Button
              android:id="@+id/btn_choose_file"
              android:layout_width="@dimen/btn_width"
              android:layout_height="@dimen/btn_height"
              android:layout_marginLeft="@dimen/btn_spacing"
              android:background="@drawable/shape_button"
              android:text="@string/item_choose_file"
              android:textColor="@color/white" />

          <TextView
              android:id="@+id/tv_file_path"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      </LinearLayout>

  </LinearLayout>

Handle File Picker

In the activity or fragment, we need to handle when the button is clicked and when a file has been chosen by user. I use fragment in this example. We need to add setOnClickListner inside which a new ACTION_GET_CONTENT Intent is created. Then call startActivityForResult with the Intent as the first parameter and a request code as the second paramter. The request code needs to be unique to distinguish it from other activity results. In order to get the result after user selecting file, we need to handle it by overriding onActivityResult

FilePickerFragment.java

  public class FilePickerFragment extends Fragment {
      public static final int PICKFILE_RESULT_CODE = 1;

      private Button btnChooseFile;
      private TextView tvItemPath;

      private Uri fileUri;
      private String filePath;

      @Override
      public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.choose_file, container, false);

          btnChooseFile = (Button) rootView.findViewById(R.id.btn_choose_file);
          tvItemPath = (TextView) rootView.findViewById(R.id.tv_file_path);

          btnChooseFile.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
                  chooseFile.setType("*/*");
                  chooseFile = Intent.createChooser(chooseFile, "Choose a file");
                  startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
              }
          });
      }

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          switch (requestCode) {
              case PICKFILE_RESULT_CODE:
                  if (resultCode == -1) {
                      fileUri = data.getData();
                      filePath = fileUri.getPath();
                      tvItemPath.setText(filePath);
                  }

                  break;
          }
      }
  }

Get the File Object

With the above code, we can get the path to the file, but not the file object. In order to get the file object, we need the user to grant permission for the application to read external storage. For Android 5.x and below, we can simply add the permission in AndroidManifest.xml.

AndroidManifest.xml

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Since Android 6.0, each permissions is asked while the application is running by showing a popup. That's what we have to do. To make it easy, we can use EasyPermission.

First, add the library to your project dependency.

app/build.gradle

  compile 'pub.devrel:easypermissions:0.2.0'

FilePickerFragment.java

  public class FilePickerFragment extends Fragment implements EasyPermissions.PermissionCallbacks {
      @Override
      public void onCreate(final Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          if (!EasyPermissions.hasPermissions(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
              EasyPermissions.requestPermissions(this, getString(R.string.permission_read_external_storage), EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE, Manifest.permission.READ_EXTERNAL_STORAGE);
          }
      }

      @Override
      public void onPermissionsGranted(int requestCode, List perms) {
          // Add your logic here
      }

      @Override
      public void onPermissionsDenied(int requestCode, List perms) {
          // Add your logic here
      }
  }

Having get the file's Uri from the previous step and permission to access external storage, it's very simple to get the File object.

  File file = new File(fileUri.getPath());

That's all about this tutorial of how to show a basic file picker in Android and get the File object.