Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.app.Activity;

import android.content.CursorLoader;

import android.database.Cursor;

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

public class Main {
    /**
     * Get the real file path from URI registered in media store 
     * @param contentUri URI registered in media store 
     */
    public static String getRealPathFromURI(Activity activity, Uri contentUri) {

        String releaseNumber = Build.VERSION.RELEASE;

        if (releaseNumber != null) {
            /* ICS, JB Version */
            if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
                // URI from Gallery(MediaStore)
                String[] proj = { MediaStore.Images.Media.DATA };
                String strFileName = "";
                CursorLoader cursorLoader = new CursorLoader(activity, contentUri, proj, null, null, null);
                Cursor cursor = cursorLoader.loadInBackground();
                if (cursor != null) {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    if (cursor.getCount() > 0)
                        strFileName = cursor.getString(column_index);

                    cursor.close();
                }
                // URI from Others(Dropbox, etc.) 
                if (strFileName == null || strFileName.isEmpty()) {
                    if (contentUri.getScheme().compareTo("file") == 0)
                        strFileName = contentUri.getPath();
                }
                return strFileName;
            }
            /* GB Version */
            else if (releaseNumber.startsWith("2.3")) {
                // URI from Gallery(MediaStore)
                String[] proj = { MediaStore.Images.Media.DATA };
                String strFileName = "";
                Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
                if (cursor != null) {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                    cursor.moveToFirst();
                    if (cursor.getCount() > 0)
                        strFileName = cursor.getString(column_index);

                    cursor.close();
                }
                // URI from Others(Dropbox, etc.) 
                if (strFileName == null || strFileName.isEmpty()) {
                    if (contentUri.getScheme().compareTo("file") == 0)
                        strFileName = contentUri.getPath();
                }
                return strFileName;
            }
        }

        //---------------------
        // Undefined Version
        //---------------------
        /* GB, ICS Common */
        String[] proj = { MediaStore.Images.Media.DATA };
        String strFileName = "";
        Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            // Use the Cursor manager in ICS         
            activity.startManagingCursor(cursor);

            cursor.moveToFirst();
            if (cursor.getCount() > 0)
                strFileName = cursor.getString(column_index);

            //cursor.close(); // If the cursor close use , This application is terminated .(In ICS Version)
            activity.stopManagingCursor(cursor);
        }
        return strFileName;
    }
}