iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android 文件选择的实现代码
  • 964
分享到

Android 文件选择的实现代码

选择Android 2022-06-06 10:06:27 964人浏览 八月长安
摘要

打开文件选择器 代码如下:private void showFileChooser() {    Intent intent = new Inte

打开文件选择器
代码如下:
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 PickerView滚动选择器的使用方法Android实现可复用的选择页面


--结束END--

本文标题: Android 文件选择的实现代码

本文链接: https://www.lsjlt.com/news/27427.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作