Get the real path from a URI - Android Network

Android examples for Network:Uri

Description

Get the real path from a 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  om*/
     * Get the real path from a URI
     * @param context Application context
     * @param contentUri Uri of the file
     * @return
     */
    public static String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME };
            cursor = context.getContentResolver().query(contentUri, proj,
                    null, null, null);
            int dataIndex = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(dataIndex);
        } finally {
            try {
                cursor.close();
            } catch (Exception e) {
            }
        }
    }
}

Related Tutorials