Android 文件选择的实现代码_安卓软件开发教程-查字典教程网
Android 文件选择的实现代码
Android 文件选择的实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:打开文件选择器复制代码代码如下:privatevoidshowFileChooser(){Intentintent=newIntent(In...

打开文件选择器

复制代码 代码如下:

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;

}

}

这个很简单。

出处:http://www.cnblogs.com/linlf03/

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类