get Media Path from Context - Android Media

Android examples for Media:Media File

Description

get Media Path from Context

Demo Code


//package com.java2s;

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

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

public class Main {
    public static String getMediaPath(Context context, Uri uri) {
        String path = null;//  w ww  .  j a  v a  2 s .c om
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri,
                    new String[] { MediaStore.Images.Media.DATA }, null,
                    null, null);
            if (cursor != null && cursor.moveToFirst()) {
                int dataColumnIndex = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                path = cursor.getString(dataColumnIndex);
            }
        } catch (Throwable ignore) {
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return path;
    }
}

Related Tutorials