android – 从sdcard marshmallow中的文件URI获取真实路径

android – 从sdcard marshmallow中的文件URI获取真实路径,第1张

概述我想选择存在于SD卡中的文件而不是内部存储器并上传它的服务器,但是我无法获得其获取其大小的路径.我已经开始使用以下代码选择文件的意图: intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); String[] m 我想选择存在于SD卡中的文件而不是内部存储器并上传它的服务器,但是我无法获得其获取其大小的路径.我已经开始使用以下代码选择文件的意图:
intent = new Intent(Intent.ACTION_OPEN_document); intent.addcategory(Intent.category_OPENABLE); intent.setType("*/*"); String[] mimetypes = {"application/*"}; intent.putExtra(Intent.EXTRA_MIME_TYPES,mimetypes); startActivityForResult(      Intent.createChooser(intent,"Select a file to Upload"),file_SELECT_CODE);

为了获取文件的路径,我使用this answer并且它正在工作,除非用户从sdcard(可移动)中选择任何文件.当我调试代码并发现该类型不是主要类型时,它不会进入这种情况:

if("primary".equalsIgnoreCase(type)){     return Environment.getExternalStorageDirectory() + "/" + split[1];}

所以我的问题是它还有什么呢?即如果类型不是主要的?在这种情况下我们如何获得文件路径?我已经搜索了许多问题和教程,其中任何一个都没有.我还尝试了其他部分this answer,但它不起作用,因为System.getenv()为“SECONDARY_STORAGE”返回null,为“EXTERNAL_STORAGE”返回sdcard.我尝试时遇到文件未找到异常:

if ("primary".equalsIgnoreCase(type)) {    return Environment.getExternalStorageDirectory() + "/" + split[1];}else{    return System.getenv("EXTERNAL_STORAGE") + "/" + split[1];}

文件的Uri和doc ID看起来像:

Uri: content://com.androID.externalstorage.documents/document/0EF9-3110%3Adevice-2016-12-02-130553.png

docID: 0EF9-3110:device-2016-12-02-130553.png

任何帮助?

解决方法 花了一些时间在AndroID设备管理器上后我找到了解决方案,这里是:

如果doc type ID不是primary,那么我使用以下命令创建路径:

filePath = "/storage/" + type + "/" + split[1];

编辑:如果是documentUri,请根据文件类型选择contentUri

这是完整的功能:

public static String getRealPathFromURI_API19(Context context,Uri uri) {    String filePath = "";    // 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];        } else {            if (Build.VERSION.SDK_INT > 20) {                    //getExternalMediaDirs() added in API 21                    file extenal[] = context.getExternalMediaDirs();                    if (extenal.length > 1) {                        filePath = extenal[1].getabsolutePath();                        filePath = filePath.substring(0,filePath.indexOf("AndroID")) + split[1];                    }             }else{                    filePath = "/storage/" + type + "/" + split[1];             }            return filePath;        }    } else if (isDownloadsdocument(uri)) {        // DownloadsProvIDer        final String ID = documentsContract.getdocumentID(uri);        //final Uri contentUri = ContentUris.withAppendedID(        // Uri.parse("content://downloads/public_downloads"),Long.valueOf(ID));        Cursor cursor = null;        final String column = "_data";        final String[] projection = {column};        try {            cursor = context.getContentResolver().query(uri,projection,null,null);            if (cursor != null && cursor.movetoFirst()) {                final int index = cursor.getColumnIndexOrThrow(column);                String result = cursor.getString(index);                cursor.close();                return result;            }        } finally {            if (cursor != null)                cursor.close();        }    } else if (documentsContract.isdocumentUri(context,uri)) {        // MediaProvIDer        String wholeID = documentsContract.getdocumentID(uri);        // Split at colon,use second item in the array        String[] IDs = wholeID.split(":");        String ID;        String type;        if (IDs.length > 1) {            ID = IDs[1];            type = IDs[0];        } else {            ID = IDs[0];            type = IDs[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[]{ID};        final String column = "_data";        final String[] projection = {column};        Cursor cursor = context.getContentResolver().query(contentUri,selection,selectionArgs,null);        if (cursor != null) {            int columnIndex = cursor.getColumnIndex(column);            if (cursor.movetoFirst()) {                filePath = cursor.getString(columnIndex);            }            cursor.close();        }        return filePath;    } else {        String[] proj = {MediaStore.Audio.Media.DATA};        Cursor cursor = context.getContentResolver().query(uri,proj,null);        if (cursor != null) {            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);            if (cursor.movetoFirst())                filePath = cursor.getString(column_index);            cursor.close();        }        return filePath;    }    return null;}
总结

以上是内存溢出为你收集整理的android – 从sdcard marshmallow中的文件URI获取真实路径全部内容,希望文章能够帮你解决android – 从sdcard marshmallow中的文件URI获取真实路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1131078.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存