
我在我的应用程序中实现了一个文件选择器,如下所示:
Intent intent = new Intent();intent.setType("*/*");intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(Intent.createChooser(intent, "Title"), file_PICK);这是一个众所周知的问题,你无法通过这种方式轻松获取实际的文件位置,因为Intent将返回一些你无法真正用来创建file对象的奇怪的Uri.
我正在使用此方法来获取实际的文件路径:
/** * 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 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // 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;}这适用于某些文件,有些则不适用.现在我注意到当我从“Downloads”文件夹中选择一个文件时它不起作用.它会跳过上面的所有if语句并返回null.
什么是正确的方法,获得实际选择的文件路径会更可靠?
解决方法:
I have a file picker implemented in my app like this
那不是“文件选择器”.这是一个内容选择器.
I need a way to be able to pick any file on AndroID device so that I can upload it to my backend.
嗯,这很大程度上取决于你在这句话中的字面意义.
ACTION_GET_CONTENT一般不是问题.但是,你得到的不一定是文件.但是,您仍然可以上传它们,假设您用于上传的任何内容都允许您从inputStream上传.如果是这样,请在ContentResolver上使用openinputStream(),传入Uri,以获取要使用的inputStream.
如果您真的需要它作为实际文件,您可以直接在您的应用程序中实现文件选择器UI. There are libraries for this.但是,您将无法访问所有文件 – 特别是,您无法使用此文件从AndroID 4.4上的removable storage上传文件.
总结以上是内存溢出为你收集整理的android – 使用ACTION_GET_CONTENT选择文件路径的正确方法全部内容,希望文章能够帮你解决android – 使用ACTION_GET_CONTENT选择文件路径的正确方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)