uri To File Path - Android Network

Android examples for Network:Uri

Description

uri To File Path

Demo Code


//package com.book2s;

import android.content.Context;

import android.database.Cursor;

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

public class Main {
    public static String uriToFilePath(Context context, String contentUri) {
        if (Uri.parse(contentUri).getScheme().equals("content")) {
            String[] p = { MediaStore.MediaColumns.DATA };
            Cursor cursor = context.getContentResolver().query(
                    Uri.parse(contentUri), p, // which columns
                    null, // which rows (all rows)
                    null, // selection args (none)
                    null); // order-by clause (ascending by name)
            if (cursor != null) {
                int iColumn = cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                if (cursor.moveToFirst()) {
                    return (cursor.getString(iColumn));
                }/*from w w  w  .java2  s.  c o m*/
            }
        }
        if (Uri.parse(contentUri).getScheme().equals("file")) {
            return Uri.parse(contentUri).getPath();
        }
        return null;
    }
}

Related Tutorials