文件选择
来源:互联网 发布:java编程思想pdf超清版 编辑:程序博客网 时间:2024/06/03 20:31
打开文件选择器
private static final int FILE_SELECT_CODE = 0;
private
void
showFileChooser() {
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT);
intent.setType(
"*/*"
);
intent.addCategory(Intent.CATEGORY_OPENABLE);
try
{
startActivityForResult( Intent.createChooser(intent,
"Select a File to Upload"
), FILE_SELECT_CODE);
}
catch
(android.content.ActivityNotFoundException ex) {
Toast.makeText(
this
,
"Please install a File Manager."
, Toast.LENGTH_SHORT).show();
}
}
选择的结果
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
switch
(requestCode) {
case
FILE_SELECT_CODE:
if
(resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String path = FileUtils.getPath(
this
, uri);
}
break
;
}
super
.onActivityResult(requestCode, resultCode, data);
}
FileUtils文件
public
class
FileUtils {
public
static
String getPath(Context context, Uri uri) {
if
(
"content"
.equalsIgnoreCase(uri.getScheme())) {
String[] projection = {
"_data"
};
Cursor cursor =
null
;
try
{
cursor = context.getContentResolver().query(uri, projection,
null
,
null
,
null
);
int
column_index = cursor.getColumnIndexOrThrow(
"_data"
);
if
(cursor.moveToFirst()) {
return
cursor.getString(column_index);
}
}
catch
(Exception e) {
// Eat it
}
}
else
if
(
"file"
.equalsIgnoreCase(uri.getScheme())) {
return
uri.getPath();
}
return
null
;
}
}
这个很简单。
阅读全文
0 0
- 选择文件、选择目录
- 选择文件
- 选择文件
- 文件选择
- 选择文件&选择储存目录
- WTL文件选择对话框多文件选择
- C#选择文件、选择文件路径
- 选择文件的对话框
- vb选择文件路径
- 文件对话框多重选择
- CFileDialog文件选择对话框
- 使用OpenFileDialog选择文件
- MFC 选择文件目录
- 弹出文件选择
- C#选择文件路径
- swt 文件 文件夹 选择
- 打开文件选择框
- MFC选择文件路径
- 三角形的面积为 area=根号下s(s−a)(s−b)(s−c) 其中s=(a+b+c)/2,abc 为三角形的三边。定义两个带参数的宏来求s,area
- [leetcode: Python]
- python ggplot使用文档(2)——gemo_abline,gemo_area,gemo_bar
- 四大组件浅析(二)——Service服务的回顾
- 使用 Selenium 将滚动条移动到目标元素位置的方法
- 文件选择
- UESTC 1584 Washi与Sonochi的约定 树状数组+排序
- mips处理器linux内核pci初始化和设备枚举详解
- ionic3/2 使用modal自定义弹出框
- 卡尔曼滤波 -- 从推导到应用
- Activiti快速入门
- Java中的取余运算和取模运算
- 数据结构-线性表
- 利用jquery写的AJAX小例子