Get the real path from the provided URI. - Android Network

Android examples for Network:Uri

Description

Get the real path from the provided URI.

Demo Code


//package com.java2s;

import android.content.Context;
import android.database.Cursor;

import android.net.Uri;
import android.provider.MediaStore;

public class Main {
    /**// w  w w  . j  a  v  a  2 s  . c  o  m
     * Get the real path from the provided URI.
     * NOTE: This will block the UI thread
     *
     * @param    uri    The URI to get the real path for
     * @param    context    The application context
     * @return The real path based on the provided URI
     */
    public static String getRealPathFromURI(Uri uri, Context context) {
        String realPath = null;
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(uri,
                filePathColumn, null, null, null);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            realPath = cursor.getString(columnIndex);
        }

        cursor.close();
        return realPath;
    }
}

Related Tutorials