如何从android pie中的内容uri获取文件路径

追风赶月莫停留,平芜尽处是春山。这篇文章主要讲述如何从android pie中的内容uri获取文件路径相关的知识,希望能为你提供帮助。
我正在尝试使用retrofit 2.0上传图像。在onActivityResult获得uri后,我试图从uri获得路径,这是给出null。相同的代码在前棒棒糖设备中完美运行。

String[] filePathColumn = { MediaStore.Image.Media.DATA }; Cursor cursor = context.getContentResolver().query(inputUri, filePathColumn, null, null, null); if (cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); // filePath is always null if (filePath != null & & !filePath.isEmpty()) { file = new File(filePath); } }

答案【如何从android pie中的内容uri获取文件路径】这是完整的工作代码,用于从内容提供商那里获取所有apis的最后一张照片:
public ArrayList< String> getNewPicturesUri(JobParameters parameters) { ArrayList< String> newPictures = new ArrayList< > (); if (parameters.getTriggeredContentAuthorities() != null) { if (parameters.getTriggeredContentUris() != null) { ArrayList< String> ids = new ArrayList< > (); for (Uri uri : parameters.getTriggeredContentUris()) { List< String> path = uri.getPathSegments(); if (path != null & & path.size() == Media.EXTERNAL_CONTENT_URI.getPathSegments().size() + 1) { ids.add(path.get(path.size() - 1)); } }if (ids.size() > 0) { StringBuilder selection = new StringBuilder(); for (int i = 0; i < ids.size(); i++) { if (selection.length() > 0) { selection.append(" OR "); } selection.append(MediaStore.Images.ImageColumns._ID); selection.append("='"); selection.append(ids.get(i)); selection.append("'"); }Cursor cursor = null; try { String[] projection = new String[]{ MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA}; cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, projection, selection.toString(), null, null); if (cursor != null) { while (cursor.moveToNext()) { newPictures.add(cursor.getString(PROJECTION_DATA)); } } } catch (SecurityException exception) { FirebaseCrash.report(exception); } finally { if (cursor != null) { cursor.close(); } } } } } return newPictures; }


    推荐阅读