国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 文件選擇的實現代碼

2020-04-11 12:03:51
字體:
來源:轉載
供稿:網友

打開文件選擇器

復制代碼 代碼如下:

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/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸水县| 偃师市| 中宁县| 永登县| 安达市| 台北市| 钟山县| 旅游| 桃源县| 原平市| 南安市| 靖西县| 长沙县| 武平县| 漯河市| 崇文区| 保山市| 昌吉市| 石林| 金阳县| 菏泽市| 抚顺县| 福海县| 县级市| 布拖县| 新安县| 荣昌县| 六盘水市| 乌什县| 玛多县| 漳州市| 清水县| 永昌县| 阳山县| 蓬安县| 罗源县| 西安市| 财经| 兖州市| 博罗县| 巴彦淖尔市|