Android URI to Path Convert getRealPathFromURI(Activity activity, Uri contentUri)

Here you can find the source of getRealPathFromURI(Activity activity, Uri contentUri)

Description

Get the real file path from URI registered in media store

Parameter

Parameter Description
contentUri URI registered in media store

Declaration


public static String getRealPathFromURI(Activity activity,
        Uri contentUri) 

Method Source Code

//package com.java2s;

import android.app.Activity;

import android.database.Cursor;

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

public class Main {
    /**//from   w w  w.  j av  a  2  s .c  o  m
     * Get the real file path from URI registered in media store 
     * @param contentUri URI registered in media store 
     */

    public static String getRealPathFromURI(Activity activity,
            Uri contentUri) {

        //---------------------
        // Undefined Version
        //---------------------
        /* GB, ICS Common */
        String[] proj = { MediaStore.Images.Media.DATA };
        String strFileName = "";
        Cursor cursor = activity.managedQuery(contentUri, proj, null, null,
                null);
        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            // Use the Cursor manager in ICS         
            activity.startManagingCursor(cursor);

            cursor.moveToFirst();
            if (cursor.getCount() > 0)
                strFileName = cursor.getString(column_index);

            //cursor.close(); // If the cursor close use , This application is terminated .(In ICS Version)
            activity.stopManagingCursor(cursor);
        }
        return strFileName;
    }
}

Related

  1. getRealPathFromURI(Activity activity, Uri contentUri)