want to pick a file or folder from your android application in sdcard / memory card or from phone memory? recently i wanted a file explorer just like windows in my android application and after a brief R&D i figured it out on how to do it below is the sample code
import java.io.File;
import java.net.URI;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class testclass extends Activity {
int PICK_REQUEST_CODE = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testclass);
// Files and directories !
Intent intent = new Intent();
Uri startDir = Uri.fromFile(new File("/sdcard"));
intent.setAction(Intent.ACTION_PICK);
intent.setDataAndType(startDir, "vnd.android.cursor.dir/*");
//intent.setDataAndType(startDir, "file://");
// Title
intent.putExtra("explorer_title", "Select a file");
// Optional colors
intent.putExtra("browser_title_background_color", "440000AA");
intent.putExtra("browser_title_foreground_color", "FFFFFFFF");
intent.putExtra("browser_list_background_color", "00000066");
// Optional font scale
intent.putExtra("browser_list_fontscale", "120%");
// Optional 0=simple list, 1 = list with filename and size, 2 = list with filename, size and date.
intent.putExtra("browser_list_layout", "2");
startActivityForResult(intent, PICK_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == PICK_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
Uri uri = intent.getData();
String type = intent.getType();
// LogHelper.i(TAG,"Pick completed: "+ uri + " "+type);
if (uri != null)
{
String path = uri.toString();
if (path.toLowerCase().startsWith("file://"))
{
// Selected file/directory path is below
path = (new File(URI.create(path))).getAbsolutePath();
}
}
}
else{}// LogHelper.i(TAG,"Back from pick with cancel status");
}
}
}
If you find any error in this code please do let us know or email me you can email id below
1 comments:
It won't work in my nexus one (2.2 Korea)
Post a Comment