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>


Download the files.

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

10 comments:

Anuj Devasthali said...

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

Andr.oid Eric said...

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.

Jesus Alberto said...

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

Thanks

Leonardo said...

thanks very much

RJ said...

thnks ....!!

Unknown said...

how to pick a directory?

Andr.oid Eric said...

To pick a directory, refer to Example of using Intent.ACTION_OPEN_DOCUMENT_TREE, to pick a directory subtree.. But it work on API Level 21 upper!

Lubomyr Shershun said...

thanks

Waseem Zafar said...

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

Andr.oid Eric said...

Hello Waseem Zafar,

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