/** 调用文件选择软件来选择文件 **/
private void showFileChooser() {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(getActivity(), "请安装文件管理器", Toast.LENGTH_SHORT)
.show();
}
}
返回的数据处理示例:
/** 根据返回选择的文件,来进行上传操作 **/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String url;
try {
url = FFileUtils.getPath(getActivity(), uri);
Log.i("ht", "url" + url);
String fileName = url.substring(url.lastIndexOf("/") + 1);
intent = new Intent(getActivity(), UploadServices.class);
intent.putExtra("fileName", fileName);
intent.putExtra("url", url);
intent.putExtra("type ", "");
intent.putExtra("fuid", "");
intent.putExtra("type", "");
getActivity().startService(intent);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void showFileChooser() {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(getActivity(), "请安装文件管理器", Toast.LENGTH_SHORT)
.show();
}
}
返回的数据处理示例:
/** 根据返回选择的文件,来进行上传操作 **/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String url;
try {
url = FFileUtils.getPath(getActivity(), uri);
Log.i("ht", "url" + url);
String fileName = url.substring(url.lastIndexOf("/") + 1);
intent = new Intent(getActivity(), UploadServices.class);
intent.putExtra("fileName", fileName);
intent.putExtra("url", url);
intent.putExtra("type ", "");
intent.putExtra("fuid", "");
intent.putExtra("type", "");
getActivity().startService(intent);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}