Open File Manager Explorer In Android Pick Any File Programmatically

How to select all extension files like .docx, pptx, xlsx, doc, pdf, jpeg, png, gif, txt, exe, apk, mkv, avi, .mp3 etc from file manager and show their path into app activity.

The method ACTION_GET_CONTENT is used to open the file manager in android with the use of Intent. By passing the right argument in it an application developer can open the default file explorer which is comes with android device pre installed . So in this tutorial we are using the intent.setType(“*/*”) method which will allow us to select any file with any extension available. After selecting file we will show its path into custom toast message so with the help of this developer can do whatever he wants to do with this file. So here is the complete step by step tutorial for Open File Manager Explorer In Android Pick Any File Programmatically .

android-project-download-code-button

How to Open File Manager Explorer In Android Pick Any File Programmatically.

Code for MainActivity.java file.

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

public class MainActivity extends AppCompatActivity {

    Button button ;
    Intent intent ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                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;

        }
    }

}

Code for activity_main.xml layout file.

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.pickfile_android_examplescom.MainActivity"
    android:background="#FFF8E1">

    <Button
        android:text="Click here to open file manager explorer and pick any file "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button" />
</RelativeLayout>

Screenshots :

pick_all_file_1

Open File Manager Explorer In Android Pick Any File Programmatically

 

pick_all_file_3

pick_all_file_4

Click here to download Open File Manager Explorer In Android Pick Any File Programmatically project with source  code.

7 Comments

  1. hi sir ,
    Thanks for these tutorial it works for images,I have error in my program..at the time of uploading text or audio file on server (database mysql) then throws
    error= E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.moraya.imageupload, PID: 4019
    java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean
    android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)’ on a null object reference

  2. Great Tutorial ! but can you help me how can i open a txt file ? here is my email [email protected] plz i need your help !!!! and thanks

  3. Thank u very much….

Leave a Reply

Your email address will not be published. Required fields are marked *