Get path of image from uri from ContentResolver - Android android.content

Android examples for android.content:ContentProvider

Description

Get path of image from uri from ContentResolver

Demo Code


//package com.java2s;

import android.content.ContentResolver;

import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore.Images;

public class Main {
    /**//from w w  w  .  jav a  2 s  .com
     * Get path of image from uri
     *
     * @param contentResolver
     * @param contentURI
     * @return path of image. Null if not found.
     */
    public static String getRealImagePathFromURI(
            ContentResolver contentResolver, Uri contentURI) {
        Cursor cursor = contentResolver.query(contentURI, null, null, null,
                null);
        if (cursor == null)
            return contentURI.getPath();
        else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(Images.ImageColumns.DATA);
            try {
                return cursor.getString(idx);
            } catch (Exception exception) {
                return null;
            }
        }
    }
}

Related Tutorials