Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore;
import android.util.Log;

public class Main {
    public static String getRealPathFromURI(Uri contentUri, Context ctx) {
        Log.d("thong", "Uri: " + contentUri.toString());

        try {
            String realpath = "";

            String[] proj = { MediaStore.Images.Media.DATA };

            //Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);

            Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);

            Log.d("thong", "Column count: " + cursor.getColumnCount());

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            cursor.moveToFirst();

            realpath = cursor.getString(column_index);

            cursor.close();

            return realpath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}