Get file path from URI

前端 未结 8 1152
你的背包
你的背包 2020-12-03 01:54

I have Uri for Image file.

I use this code for gets file path from Uri:

public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
         


        
8条回答
  •  心在旅途
    2020-12-03 02:33

    Solution:

        public class RealPathUtil {
    
        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API19(Context context, Uri uri){
            String filePath = "";
            String wholeID = DocumentsContract.getDocumentId(uri);
    
             // Split at colon, use second item in the array
             String id = wholeID.split(":")[1];
    
             String[] column = { MediaStore.Images.Media.DATA };     
    
             // where id is equal to             
             String sel = MediaStore.Images.Media._ID + "=?";
    
             Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                       column, sel, new String[]{ id }, null);
    
             int columnIndex = cursor.getColumnIndex(column[0]);
    
             if (cursor.moveToFirst()) {
                 filePath = cursor.getString(columnIndex);
             }   
             cursor.close();
             return filePath;
        }
    
    
        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
              String[] proj = { MediaStore.Images.Media.DATA };
              String result = null;
    
              CursorLoader cursorLoader = new CursorLoader(
                      context, 
                contentUri, proj, null, null, null);        
              Cursor cursor = cursorLoader.loadInBackground();
    
              if(cursor != null){
               int column_index = 
                 cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
               cursor.moveToFirst();
               result = cursor.getString(column_index);
              }
              return result;  
        }
    
        public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
                   String[] proj = { MediaStore.Images.Media.DATA };
                   Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
                   int column_index
              = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                   cursor.moveToFirst();
                   return cursor.getString(column_index);
        }
    }
    

    http://hmkcode.com/android-display-selected-image-and-its-real-path/

提交回复
热议问题