Gets real path from the URI - Android Network

Android examples for Network:Uri

Description

Gets real path from the 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  va 2  s.c  o m
     * Gets real path from the URI
     * @param context
     * @param contentUri
     * @return
     */
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj,
                    null, null, null);

            if (cursor == null)
                return null;

            if (cursor.getColumnIndex(MediaStore.Images.Media.DATA) != -1) {
                int columnIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }

            return null;
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
}

Related Tutorials