get Real Path From URI - Android Network

Android examples for Network:Uri

Description

get Real Path From 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 {
    /***/*from w  w w. j  ava2 s  . c  o m*/
     * 
     * @param contentURI
     * @param context
     * @return absolute path for given URI
     */
    public static String getRealPathFromURI(Uri contentURI, Context context) {
        String result;
        Cursor cursor = context.getContentResolver().query(contentURI,
                null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }
}

Related Tutorials