Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 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));
                }
            }
        }
        if (Uri.parse(contentUri).getScheme().equals("file")) {
            return (Uri.parse(contentUri).getPath());
        }
        return (null);
    }
}