android内外存储路径获取

来源:互联网 发布:淘宝店铺流失金额影响 编辑:程序博客网 时间:2024/05/17 07:46

最近使用android上传文件功能时遇到了uri获取不到绝对路径的问题,在网上查了好多方法,但是因为android手机机型比较多,因此通过单独判断uri来拼接地址不太好,因此找到了一个反射的方法来拼接路径,以上传txt文本为例。

打开txt文本的方法,记得加权限。

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                intent.setType("text/plain");                intent.addCategory(Intent.CATEGORY_OPENABLE);                startActivityForResult(intent, 1);

获取内存卡的绝对路径,isMove是true的时候是外置内存卡,false内置存储

 private String getFilePath(boolean isMove) {        StorageManager mStorageManager = (StorageManager) getApplication().getSystemService(Context.STORAGE_SERVICE);        Class<?> storageVolumeClazz = null;        try {            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");            Method getPath = storageVolumeClazz.getMethod("getPath");            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");            Object result = getVolumeList.invoke(mStorageManager);            final int length = Array.getLength(result);            for (int i = 0; i < length; i++) {                Object storageVolumeElement = Array.get(result, i);                String pathFile = (String) getPath.invoke(storageVolumeElement);                boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);                if (isMove == removable) {                    return pathFile;                }            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        }        return null;    }

回调里面的代码

 @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        switch (requestCode) {            case 1:                if (resultCode == RESULT_OK) {                    if (data != null) {                        Uri uri = data.getData();                        String finalPath = null;                        String[] urls = uri.getPath().toString().split(":");                        finalPath = getFilePath(false) + File.separator                                + urls[1].toString().trim();                        if (new File(finalPath).exists() == true) {                        } else {                            finalPath = getFilePath(true) + File.separator                                    + urls[1].toString().trim();                        }                        if (finalPath != null) {                            File file = new File(finalPath);                            FileInputStream f = null;                            try {                                f = new FileInputStream(file);                                byte[] buf = new byte[1024];                                int hasRead = 0;                                StringBuilder sb = new StringBuilder("");                                while (((hasRead = f.read(buf)) > 0)) {                                    sb.append(new String(buf, 0, hasRead));                                }                                Log.i("aa---", sb.toString());                                f.close();                            } catch (FileNotFoundException e) {                                e.printStackTrace();                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                    } else {                        tv_checkTv.setText("上传出现错误,请重新上传!");                    }                }        }    }

但是这里还有个一个问题就是不好判断选择的文件是外置存储还是内置存储的,我这里是先假设是内置存储,然后判断一下文件是否存在,如果不存在那文件肯定就是在外置存储。但是假如两个存储中存在同样的文件名称的话就会出现问题,因此大家有什么好的方法,一起讨论一下。

1 0
原创粉丝点击