安卓打开文件浏览器,选择文件后得到返回路径

新手,写的不对还望指正!

在安卓应用开发中经常会遇到需要打开系统文件管理器选择文件后返回路径的操作。例如点击一个导入的Button按钮,首先在根目录下寻找所需要的文件,若文件不存在就弹出对话框是否选择文件,选择文件后返回文件路径,给Button注册监听:

public void onClick(View arg0) {

if (mFilePath.equals("没有找到相关文件")) {

AlertDialog.Builder builder = new AlertDialog.Builder(FeildListActivity.this);

builder.setCancelable(false);

builder.setTitle("提示")

.setMessage("该目录下文件不存在是否从别的目录下寻找?")

.setPositiveButton("是",new DialogInterface.OnClickListener() {

@Override

public void onClick(

DialogInterface dialog,int which) {

// 打开系统文件浏览功能

Intent intent = new Intent();

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");

intent.addCategory(Intent.CATEGORY_OPENABLE);

startActivityForResult(intent,INFILE_CODE);

}

})

.setNegativeButton("否",null})

.show();

}

重写onActivityResult函数,在函数内部获得返回路径,代码如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode != Activity.RESULT_OK) {

finish();

} else if (requestCode == INFILE_CODE) {

mFilePath = Uri.decode(data.getDataString());

//通过data.getDataString()得到的路径如果包含中文路径,则会出现乱码现象,经过Uri.decode()函数进行解码,得到正确的路径。但是此时路径为Uri路径,必须转换为String路径,网上有很多方法,本人通过对比发现,Uri路径里多了file://字符串,所以采用以下方法将前边带的字符串截取掉,获得String路径,可能通用性不够好,下一步会学习更好的方法。

mFilePath = mFilePath.substring(7, mFilePath.length());

}

}

时间: 2024-08-15 06:08:14

安卓打开文件浏览器,选择文件后得到返回路径的相关文章

C# 选择文件夹 选择文件

选择文件 1 //选择文件 2 OpenFileDialog dialog = new OpenFileDialog(); 3 dialog.Multiselect = true;//该值确定是否可以选择多个文件 4 dialog.Title = "请选择文件夹"; 5 dialog.Filter = "所有文件(*.*)|*.*"; 6 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)

弹出打开/保存文件对话框 选择文件夹对话框

打开/保存文件对话框: CFileDialog::CFileDialog( BOOL bOpenFileDialog, //为TRUE则显示打开对话框,为FALSE则显示保存对话文件对话框 LPCTSTR lpszDefExt = NULL, //默认的文件扩展名 LPCTSTR lpszFileName = NULL, //默认的文件名 DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //设定风格 LPCTSTR lpszFilt

C#选择文件、选择文件夹、打开文件(或者文件夹)

转载自:http://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowD

打开窗口进行选择文件(txt文件),打开所选文件,读入文件

用mfc编写项目的时候往往需要调用窗口,允许用户通过窗口进行选择文件操作 TCHAR szBuffer[MAX_PATH] = { 0 }; OPENFILENAME ofn = { 0 }; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = m_hWnd; ofn.lpstrFilter = _T("txt文件(*.txt)\0");//要选择的文件后缀 char buf[80]; getcwd(buf, sizeof(buf)); ofn

C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!

https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowDial

c#winform选择文件,文件夹,打开指定目录方法

private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter="所有文件(*.*)|*.*"; if (fileDialog.ShowDialog() == Dia

c# 代码实现打开文件 文件夹以及选择文件功能

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TestFolderBrowserDialog { public partial class Form1 : Form { public Form1(

使用C#选择文件夹、打开文件夹、选择文件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TestFolderBrowserDialog { public partial class Form1 : Form { public Form1(

C# winform文件批量转编码 选择文件夹

C# winform文件批量转编码 选择文件夹 打开指定目录 private void btnFile_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter="所有文件(*.*)|*.*"; if