Sunday, March 27, 2011

Pick a file using Intent.ACTION_GET_CONTENT

If you have to pick a file in your app, but you don't want to implement your own file explorer; you can start a activity with intent of Intent.ACTION_GET_CONTENT. Android OS will choice a suitable app with such function for you.

Pick a file using Intent.ACTION_GET_CONTENT

package com.exercise.AndroidPick_a_File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidPick_a_File extends Activity {
 
 TextView textFile;
 
 private static final int PICKFILE_RESULT_CODE = 1;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
      
       Button buttonPick = (Button)findViewById(R.id.buttonpick);
       textFile = (TextView)findViewById(R.id.textfile);
      
       buttonPick.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
             intent.setType("file/*");
       startActivityForResult(intent,PICKFILE_RESULT_CODE);
    
   }});
   }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  switch(requestCode){
  case PICKFILE_RESULT_CODE:
   if(resultCode==RESULT_OK){
    String FilePath = data.getData().getPath();
    textFile.setText(FilePath);
   }
   break;
   
  }
 }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<Button
   android:id="@+id/buttonpick"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="- PICK a file -"
   />
<TextView 
   android:id="@+id/textfile"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>

Related:
- More for Pick a file using Intent.ACTION_GET_CONTENT


Updated@2017-06-19:
Please read update post about open document:
Using Intent.ACTION_OPEN_DOCUMENT, for KitKat API 19 or higher
Open images with Intent.ACTION_OPEN_DOCUMENT, Intent.ACTION_GET_CONTENT and Intent.ACTION_PICK
Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, with checking and requesting permission at runtime


12 comments:

  1. Good tutorial.. But gives error if there is no External File Explorer Installed:

    ReplyDelete
  2. hello Anuj Devasthali,

    yes, this example assume user have install file explorer apps to handle intent of ACTION_GET_CONTENT. But it's not include in default Android system.

    Or you have to implement your own File Explorer. It's a very simple example, may be you have to do much more to make it as a completed app.

    ReplyDelete
  3. How to specify a directory path so Astro starts from there on?

    Thanks

    ReplyDelete
  4. i want to detect jerk through mobile sensor any body can help me

    ReplyDelete
  5. Hello Waseem Zafar,

    May be this help: http://developer.android.com/guide/topics/sensors/sensors_motion.html

    ReplyDelete
  6. how to pick a single document

    ReplyDelete
  7. hello Anushka Mathur,

    what you means "a single document"?

    Anyway, please check the link under Updated@2017-06-19 in the post.

    ReplyDelete