private File uriToFile(Uri uri) {
String img_path;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null,
null, null);
if (actualimagecursor == null) {
img_path = uri.getPath();
} else {
int actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
img_path = actualimagecursor.getString(actual_image_column_index);
}
File file = new File(img_path);
return file;
}
private Uri fileToUri(File file){
if (file != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file);
} else {
return Uri.fromFile(file);
}
}
return null;
}