Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file path from URI

I have Uri for Image file.

I use this code for gets file path from Uri:

public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = mContext.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        Log.message(e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

If I choose image from Gallery app(Android 4.4.2, arm-emu),

uri.getPath = /external/images/media/16 and it work's fine (My file path: /storage/sdcard/Download/0_cf15a_7800a7e5_orig.jpg)

If I choose image from Documents app(Android 4.4.2, arm-emu),

 I have uri.getPath = /document/image:16 and function getRealPathFromURI returns null.

How I can return correct path to file for boths action?

My Code Is:-

    @Override
    public void onClick(View v) {
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Photohunt" + File.separator);
        root.mkdirs();
        final String fname = Common.getUniqueImageFilename();
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
            // Camera.
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = mContext.getPackageManager();
            final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for(ResolveInfo res : listCam) {
                final String packageName = res.activityInfo.packageName;
                final Intent intent = new Intent(captureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                cameraIntents.add(intent);
            }

            // Filesystem.
            final Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            // Chooser of filesystem options.
            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

            // Add the camera options.
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

            startActivityForResult(chooserIntent, PICTURE_REQUEST_CODE);
    }

Handle activity result:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == Activity.RESULT_OK)
    {
        if(requestCode == PICTURE_REQUEST_CODE)
        {
            final boolean isCamera;
            if(data == null)
            {
                isCamera = true;
            }
            else
            {
                final String action = data.getAction();
                if(action == null)
                {
                    isCamera = false;
                }
                else
                {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if(isCamera)
            {
                selectedImageUri = outputFileUri;
            }
            else
            {
                selectedImageUri = data == null ? null : data.getData();
            }

            Log.variable("uri", selectedImageUri.getPath());
            ConfirmImageFragment fragment = new ConfirmImageFragment(selectedImageUri, mContestId);
            FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.main_container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}

Loading selected file into ImageView works fine for both state:

private void loadImage() {
    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), mUri);
        mImage.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
like image 713
Artem Avatar asked Nov 17 '14 03:11

Artem


4 Answers

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/

like image 177
Artem Avatar answered Oct 19 '22 20:10

Artem


A simpler version of the accepted answer that will detect the API level and use the correct method :

@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getFilePath(Context context, Uri uri)
{
    int currentApiVersion;
    try
    {
         currentApiVersion = android.os.Build.VERSION.SDK_INT;
    }
    catch(NumberFormatException e)
    {
        //API 3 will crash if SDK_INT is called
        currentApiVersion = 3;
    }
    if (currentApiVersion >= Build.VERSION_CODES.KITKAT)
    {
        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;
    }
    else if (currentApiVersion <= Build.VERSION_CODES.HONEYCOMB_MR2 && currentApiVersion >= Build.VERSION_CODES.HONEYCOMB)

    {
        String[] proj = {MediaStore.Images.Media.DATA};
        String result = null;

        CursorLoader cursorLoader = new CursorLoader(
                context,
                uri, 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;
    }
    else
    {

        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
        int column_index
                = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}
like image 32
yehyatt Avatar answered Oct 19 '22 20:10

yehyatt


Here's my solution, using all tricks I've found so far, which I use on my own app too (here) :

FileUtilEx.kt

fun Closeable?.closeSilently() {
    if (this != null) try {
        this.close()
    } catch (e: Exception) {
    }
}

object FileUtilEx {
    fun getFilePathFromUri(context: Context, uri: Uri, includeUriMappingTechnique: Boolean = true, tryToGetWritePermission: Boolean = false): ClosableFileHolder? {
        var file: File
        uri.path?.let {
            file = File(it)
            if (file.exists() && file.canRead()) {
//                Log.d("AppLog", "got real file")
                return ClosableFileHolder(file)
            }
        }
        if (uri.scheme == "file") {
            try {
                val jUri = java.net.URI(uri.scheme, uri.schemeSpecificPart, uri.fragment)
                file = File(jUri)
                if (file.exists() && file.canRead()) {
//                    Log.d("AppLog", "got real file")
                    return ClosableFileHolder(file)
                }
            } catch (e: Exception) {
            }
            try {
                val uriStr = uri.toString()
                val decodePath = URLDecoder.decode(uriStr, "UTF-8")
                file = File(decodePath)
                if (file.exists() && file.canRead()) {
//                    Log.d("AppLog", "got real file")
                    return ClosableFileHolder(file)
                }
            } catch (e: UnsupportedEncodingException) {
            }
        }
        val authority = uri.authority
        if (uri.scheme == "content") {
            //handles "Files" app, and many others
            getRealPathFromUri(context, uri)?.let {
                file = File(it)
                if (file.exists() && file.canRead()) {
//                    Log.d("AppLog", "got real file")
                    return ClosableFileHolder(file)
                }
            }
        }
        if (includeUriMappingTechnique) {
            val fileUsingUriMappingTechnique = getFileUsingUriMappingTechnique(context, uri, tryToGetWritePermission)
            if (fileUsingUriMappingTechnique != null)
                return fileUsingUriMappingTechnique
        }
        getFilePathFromDocumentUri(context, uri)?.let { filePath ->
            file = File(filePath)
            if (file.exists() && file.canRead())
                return ClosableFileHolder(file)
        }
        return null
    }


    private fun getRealPathFromUri(context: Context, contentUri: Uri): String? {
        try {
            context.contentResolver.query(contentUri, arrayOf(MediaStore.Images.Media.DATA), null, null, null)?.use { cursor ->
                val columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
                if (columnIndex < 0 || cursor.count <= 0)
                    return null
                cursor.moveToFirst()
                return cursor.getString(columnIndex)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun getFileUsingUriMappingTechnique(context: Context, androidUri: Uri, tryToGetWritePermission: Boolean = false): ClosableFileHolder? {
        var parcelFileDescriptor: ParcelFileDescriptor? = null
        for (i in 0..1)
            try {
                parcelFileDescriptor = context.contentResolver.openFileDescriptor(androidUri, if (tryToGetWritePermission && i == 0) "w" else "r")
                if (parcelFileDescriptor != null) {
                    val fd: Int = parcelFileDescriptor.fd
                    val linkFileName = "/proc/self/fd/$fd"
                    if (VERSION.SDK_INT >= VERSION_CODES.O) {
                        val realFilePath = Files.readSymbolicLink(Paths.get(linkFileName))
                        val file = realFilePath.toFile()
                        if (file.exists() && file.canRead()) {
                            parcelFileDescriptor.closeSilently()
                            return ClosableFileHolder(file)
                        }
                    }
                    val file = File(linkFileName)
                    if (file.exists() && file.canRead())
                        return ClosableFileHolder(file, parcelFileDescriptor)
                    parcelFileDescriptor.closeSilently()
                }
            } catch (e: Exception) {
                parcelFileDescriptor.closeSilently()
                parcelFileDescriptor = null
            }
        return null
    }

    private fun getFilePathFromDocumentUri(context: Context, uri: Uri): String? {
        //            https://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri
        // DocumentProvider
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT && DocumentFile.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if ("com.android.externalstorage.documents" == uri.authority) {
                val docId = DocumentsContract.getDocumentId(uri)
                val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                val type = split[0]
                // This is for checking Main Memory
                return if ("primary".equals(type, ignoreCase = true)) {
                    if (split.size > 1) {
                        Environment.getExternalStorageDirectory().absolutePath + "/" + split[1] + "/"
                    } else {
                        Environment.getExternalStorageDirectory().absolutePath + "/"
                    }
                    // This is for checking SD Card
                } else {
                    "storage" + "/" + docId.replace(":", "/")
                }
            }
        }
        return null
    }
}

ClosableFileHolder.kt

class ClosableFileHolder(val file: File, private val parcelFileDescriptor: ParcelFileDescriptor? = null) : Closeable {

    fun isUsingUriMappingTechnique(): Boolean = parcelFileDescriptor != null

    override fun close() {
        parcelFileDescriptor.closeSilently()
    }

    protected fun finalize() {
        parcelFileDescriptor.closeSilently()
    }
}

Usage:

FileUtilEx.getFilePathFromUri(context, uri, false)?.use {
    val file = it.file
    ...
}

The tryToGetWritePermission parameter is in case you want to access the file, but it's in fact not available normally (like in SAF), so it's offered you in a different place. This is useful in case you want to access the file but don't care where it really exists.

like image 2
android developer Avatar answered Oct 19 '22 18:10

android developer


Make sure you give permission in Manifest. Wasted 2 hours trying various methods without giving permissions :/

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
like image 1
Haroon Rasheed Avatar answered Oct 19 '22 18:10

Haroon Rasheed