get Path From Uri - Android Network

Android examples for Network:Uri

Description

get Path From Uri

Demo Code


//package com.java2s;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

public class Main {

    public static String getPathFromUri(Context context, Uri contentUri) {
        if (contentUri != null) {
            if (contentUri.getScheme().toString().compareTo("content") == 0) {
                String[] proj = { MediaStore.Images.Media.DATA };
                CursorLoader loader = new CursorLoader(context, contentUri,
                        proj, null, null, null);
                Cursor cursor = loader.loadInBackground();
                int index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();/* w w w .j a  va2  s  . c  o  m*/
                return cursor.getString(index);
            } else if (contentUri.getScheme().toString().compareTo("file") == 0) {
                String fileName = contentUri.toString().replace("file://",
                        "");
                return fileName;
            }
        }
        return null;
    }
}

Related Tutorials