In the project, you need to convert Uri to an absolute path. Find a way online and take a note.
There are many methods online, but some are invalid for versions after 4.4. The method here can convert Uri to absolute path in versions after 4.4.
public class GetPathFromUri { /** * Get absolute paths from Uri, designed for Android 4.4 */ public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = .SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && (context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = (uri); final String[] split = (":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return () + "/" + split[1]; } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = (uri); final Uri contentUri = ( ("content://downloads/public_downloads"), (id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = (uri); final String[] split = (":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = .EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = .EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = .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(())) { return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(())) { return (); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = ().query(uri, projection, selection, selectionArgs, null); if (cursor != null && ()) { final int column_index = (column); return (column_index); } } finally { if (cursor != null) (); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "".equals(()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "".equals(()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "".equals(()); } }
Absolute path to Uri is simpler
Create a File object with an absolute path and call it
(file)
The above is the example code for converting Android Uri and file paths to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!