Android Pick File Programatically

1
554

Hello, Developers! In this tutorial, we will learn how to Pick any File Programmatically. You can fetch any kind of file like a jpg or pdf etc. all by using Intent. On a Button click, Intent will launch your file Browser where all files of different formats will be available. You can pick any one file of your choice from there. You can choose what you may like to do with that file according to your needs. We did a similar implementation earlier where we fetched an image via Gallery or Camera and set that to an ImageView. You may check that Tutorial out from here.

The UI of the app will look something like this:

 

 

 

 

 

 

 

 

 

 

 

 

Download Project -[media-downloader media_id=”2180″]

Download APK – [media-downloader media_id=”2181″]

Wants to Learn Advanced Android Application development from scratch- Beyond Basics

Creating New Project  –

Step 1: – Creating Project

Open your Android Studio -> Create a new project -> Choose BlankActivity > Finish

Step 2: Creating Layout -> activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:text="File Picker "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_picker" />
</RelativeLayout>

We need just one Button to launch our code, or to open the File Browser.

Related:

Uploading file to Google Drive in Android Application

How to read PDF files in android

Play a music file in background using services

Downloading File From Android WebView

Audio Recording,Play and Stop Android Tutorial

Step 3: Creating MainActivity.java

package com.mytrendin.pickanyfile;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.btn_picker);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, 7);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        switch (requestCode) {
            case 7:
                if (resultCode == RESULT_OK) {
                    String PathHolder = data.getData().getPath();
                    Toast.makeText(MainActivity.this, PathHolder, Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}

We used startActivityForResult() because we are starting another Activity to receive a result back. We did a similar usage when we launched the camera to capture the image and set it to the ImageView. you can see that code from our another blog from here.

Conclusion:

This is a very common requirement for Android Developers to pick a file programmatically.This has following uses for ex:

  1. Fetching image from gallery and setting in ImageView
  2. Fetching PDF and sending E-mail
  3. Uploading any file to the server

You can download the code and the APK File from the Links given above.In Case of Any queries you can leave a comment below and also you may give suggestions for our next blog! Keep following! If You are Beginner Learn Android application development from the scratch


If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

1 COMMENT

  1. Hello I like your tutorials very much because of the simplicity of it …

    I would like to request you to make a tutorial on how to make a custom File browser……Yes, i have seen other tutorials on it but i didn’t understood anything……So, i would like to request you to make a tutorial on making a custom file browser……

    I want to learn it because i am trying to make a music player app and i want a button which lets users choose their audio file from internal storage as well as external storage……

    I would request you to keep it most simple …and explain every code(I mean which class does what..as i am new to android programming)…

    Thanks in advance!!!!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here