// метод возвращает полный реальный путь до файла, включая имя и расширение
public static String getFilePath(Uri uri) {
Context context = RawPrinterApp.getAppContext();
try {
String selection = null;
String[] selectionArgs = null;
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
uri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
switch (type) {
case "image":
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
break;
case "video":
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
break;
case "audio":
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
break;
}
selection = "_id=?";
selectionArgs = new String[]{split[1]};
}
}
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {MediaStore.Images.Media.DATA};
try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {
int column_index = Objects.requireNonNull(cursor).getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (NullPointerException e) {
RawPrinterApp.showToast("error : " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
RawPrinterApp.showToast("error : " + e.getMessage());
Log.d("RAWBT", "Ignore Exception");
e.printStackTrace();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}catch (Exception e){
RawPrinterApp.showToast("error : "+e.getMessage());
e.printStackTrace();
}
return "";
}