Android File Picker Example

Android File Picker Example
Android File Picker

Android File Picker, Do you want to show document picker to your Android. Software to allow customers choosing report in their neighborhood storage? This educational indicates you the way to reveal. A simple document picker and get the File object of decided on file.


First, create the XML layout. To preserve this tutorial simple, there may be best a Button. So that it will trigger to expose the file picker whilst it’s clicked and a TextView for showing the direction of decided on file.


<LinearLayout xmlns:android=""
      android:orientation="vertical" android:layout_width="match_parent"
      android:padding="20dp" >
          android:orientation="horizontal" >
              android:textColor="@color/white" />
              android:layout_height="wrap_content" />

Handle File Picker

In the hobby or fragment. We want to handle while the button is clicked. And while a record has been chosen through person. I use fragment in this situation. We need to add setOnClickListner internal which a brand new ACTION_GET_CONTENT Intent is created. Then name startActivityForResult with the Intent as the first parameter and a request code as the second paramter. The request code desires to be particular to distinguish it from other hobby outcomes. In order to get the result after consumer deciding on report, we need to address it via overriding onActivityResult.

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;
      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(;
          tvItemPath = (TextView) rootView.findViewById(;
          btnChooseFile.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
                  Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
                  chooseFile = Intent.createChooser(chooseFile, "Choose a file");
                  startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          switch (requestCode) {
              case PICKFILE_RESULT_CODE:
                  if (resultCode == -1) {
                      fileUri = data.getData();
                      filePath = fileUri.getPath();

Get the File Object

With the above code, we are able to get the course to the record, however no longer the file item. In order to get the report object. We want the person to supply permission for the software to study external garage. For Android 5.X and under, we will genuinely upload the permission in AndroidManifest.Xml.


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

Since Android 6.0, each permissions is requested at the same time. As the application is walking with the aid of showing a popup. That’s what we have to do. To make it smooth, we will use EasyPermission.

First, add the library on your assignment dependency.


compile 'pub.devrel:easypermissions:0.2.0'

public class FilePickerFragment extends Fragment implements EasyPermissions.PermissionCallbacks {
      public void onCreate(final Bundle 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);
      public void onPermissionsGranted(int requestCode, List perms) {
          // Add your logic here
      public void onPermissionsDenied(int requestCode, List perms) {
          // Add your logic here

Having get the file’s Uri from the previous step and permission. To get entry to external storage, it’s very simple to get the File item.

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

That’s all approximately this tutorial of how to show a simple report picker in Android and get the File item.