手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android 文件选择的实现代码
Android 文件选择的实现代码
摘要:打开文件选择器复制代码代码如下: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/

【Android 文件选择的实现代码】相关文章:

android开发基础教程—文件存储功能实现

android 九宫格滑动解锁开机实例源码学习

Android 九宫格的实现方法

Android ViewPager相册横向移动的实现方法

Android定制RadioButton样式三种实现方法

基于Android SQLite的使用介绍

Android 倒影算法的实现代码

Android使用Pull解析器解析xml文件的实现代码

Android裁剪图片为圆形图片的实现原理与代码

基于Android中手势交互的实现方法

精品推荐
分类导航