//调用该方法从设备内存中选择文件
static Uri url;
String path;
private void showFileChooser() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
i.addCategory(Intent.CATEGORY_OPENABLE);
i = Intent.createChooser(i, "Choose a file");
startActivityForResult(i, FILE_SELECT_CODE);
}
//这里你会得到所选文件的路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==-1)
{
Log.w("Request Code", ""+requestCode);
Log.w("Result Code", ""+resultCode);
path = getRealPathFromURI( data.getData());
try {
Log.w("Intent", data.getData().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.w("Error", e.toString());
}
}
}
//获取文件的准确路径 -- 准确路径保存在path变量中
public String getRealPathFromURI (Uri contentUri)
{
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
if("content".equalsIgnoreCase(contentUri.getScheme ()))
{
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
else if("file".equalsIgnoreCase(contentUri.getScheme()))
{
return contentUri.getPath();
}
return null;
}