Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.annotation.SuppressLint;
import android.content.Context;

import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class Main {
    @SuppressLint("NewApi")
    public static String getRealPathFromUriApi19(Context context, Uri uri) {
        String filePath = "";
        String wholeId = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeId.split(":")[1];

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

        // where id is equal to
        String selection = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
                selection, new String[] { id }, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    }
}