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:
Good tutorial.. But gives error if there is no External File Explorer Installed:
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.
How to specify a directory path so Astro starts from there on?
Thanks
thanks very much
thnks ....!!
how to pick a directory?
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!
thanks
i want to detect jerk through mobile sensor any body can help me
Hello Waseem Zafar,
May be this help: http://developer.android.com/guide/topics/sensors/sensors_motion.html
how to pick a single document
hello Anushka Mathur,
what you means "a single document"?
Anyway, please check the link under Updated@2017-06-19 in the post.
Post a Comment