我正在尝试使用翻新版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);
}
}
答案 0 :(得分:0)
这里是完全有效的代码,用于从使用所有api的内容提供商那里拍摄最后一张照片:
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;
}