贴出 关键代码:

public void openSystemFile(){        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);        intent.setType("*/*");        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);//打开多个文件        intent.addCategory(Intent.CATEGORY_DEFAULT);        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);        try{            startActivityForResult(Intent.createChooser(intent,"请选择文件"),1);        }catch (ActivityNotFoundException e){            e.printStackTrace();            Toast.makeText(this,"请 安装文件管理器",Toast.LENGTH_SHORT);        }    }
@Override    public void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {            //Get the Uri of the selected file                        if(data.getClipData() != null) {//有选择多个文件                int count = data.getClipData().getItemCount();                Log.i(TAG,"url count :  "+ count);                int currentItem = 0;                while(currentItem < count) {                    Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();                    String imgpath = ContentUriUtil.getPath(this,imageUri);                    Log.i(TAG,"url "+ imgpath);                                        //do something with the image (save it to some directory or whatever you need to do with it here)                    currentItem = currentItem + 1;                }            } else if(data.getData() != null) {//只有一个文件咯                String imagePath = ContentUriUtil.getPath(this,data.getData());                Log.i(TAG,"Single image path ---- "+ imagePath);                                //do something with the image (save it to some directory or whatever you need to do with it here)            }        }    }

 

 

package com.xx.utils;import android.content.ContentUris;import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.provider.DocumentsContract;import android.provider.MediaStore;public class ContentUriUtil {    /**     * Get a file path from a Uri. This will get the the path for Storage Access     * Framework Documents, as well as the _data field for the MediaStore and     * other file-based ContentProviders.     *     * @param context The context.     * @param uri The Uri to query.     * @author paulburke     */    public static String getPath(final Context context, final Uri uri) {        final boolean isKitKat = true;        // DocumentProvider        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {            // ExternalStorageProvider            if (isExternalStorageDocument(uri)) {                final String docId = DocumentsContract.getDocumentId(uri);                final String[] split = docId.split(":");                final String type = split[0];                if ("primary".equalsIgnoreCase(type)) {                    return Environment.getExternalStorageDirectory() + "/" + split[1];                }                // TODO handle non-primary volumes            }            // DownloadsProvider            else if (isDownloadsDocument(uri)) {                final String id = DocumentsContract.getDocumentId(uri);                final Uri contentUri = ContentUris.withAppendedId(                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));                return getDataColumn(context, contentUri, null, null);            }            // MediaProvider            else if (isMediaDocument(uri)) {                final String docId = DocumentsContract.getDocumentId(uri);                final String[] split = docId.split(":");                final String type = split[0];                Uri contentUri = null;                if ("image".equals(type)) {                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;                } else if ("video".equals(type)) {                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;                } else if ("audio".equals(type)) {                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;                }                final String selection = "_id=?";                final String[] selectionArgs = new String[] {                        split[1]                };                return getDataColumn(context, contentUri, selection, selectionArgs);            }        }        // MediaStore (and general)        else if ("content".equalsIgnoreCase(uri.getScheme())) {            // Return the remote address            if (isGooglePhotosUri(uri))                return uri.getLastPathSegment();            return getDataColumn(context, uri, null, null);        }        // File        else if ("file".equalsIgnoreCase(uri.getScheme())) {            return uri.getPath();        }        return null;    }    /**     **     * Get the value of the data column for this Uri. This is useful for     * MediaStore Uris, and other file-based ContentProviders.     *     * @param context The context.     * @param uri The Uri to query.     * @param selection (Optional) Filter used in the query.     * @param selectionArgs (Optional) Selection arguments used in the query.     * @return The value of the _data column, which is typically a file path.     */    public static String getDataColumn(Context context, Uri uri, String selection,                                       String[] selectionArgs) {        Cursor cursor = null;        final String column = "_data";        final String[] projection = {                column        };        try {            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,                    null);            if (cursor != null && cursor.moveToFirst()) {                final int index = cursor.getColumnIndexOrThrow(column);                return cursor.getString(index);            }        } finally {            if (cursor != null)                cursor.close();        }        return null;    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is ExternalStorageProvider.     */    public static boolean isExternalStorageDocument(Uri uri) {        return "com.android.externalstorage.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is DownloadsProvider.     */    public static boolean isDownloadsDocument(Uri uri) {        return "com.android.providers.downloads.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is MediaProvider.     */    public static boolean isMediaDocument(Uri uri) {        return "com.android.providers.media.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is Google Photos.     */    public static boolean isGooglePhotosUri(Uri uri) {        return "com.google.android.apps.photos.content".equals(uri.getAuthority());    }}

 

更多相关文章

  1. android异步操作AsyncTask编写文件查看器
  2. Android http文件上传-本地+服务器一条龙分析
  3. Android 可選文件格式瀏覽器
  4. Android操作SD卡文件
  5. Android 之 将RAW资源文件写入SD卡工具类
  6. Android 上传文件[转]
  7. 挂载android只读文件系统为可读写
  8. android 之 读取本地json文件返回string
  9. android中利用pull解析xml文件

随机推荐

  1. Android(安卓)Studio 常见问题集锦
  2. Custom Dialog !自定义对话框
  3. android实现异步加载图片
  4. 安卓入门.控件样式
  5. Layout_weight的特性
  6. Android手机开发ubuntu系统中常用的搜索
  7. android检测网络连接状态示例讲解
  8. Android(安卓)Google Maps 完整实例分析
  9. Android(安卓)启动另外activity并返回结
  10. android中ViewPager 与fragment 的一个应