Example usage for android.app Activity managedQuery

List of usage examples for android.app Activity managedQuery

Introduction

In this page you can find the example usage for android.app Activity managedQuery.

Prototype

@Deprecated
public final Cursor managedQuery(Uri uri, String[] projection, String selection, String[] selectionArgs,
        String sortOrder) 

Source Link

Document

Wrapper around ContentResolver#query(android.net.Uri,String[],String,String[],String) that gives the resulting Cursor to call #startManagingCursor so that the activity will manage its lifecycle for you.

Usage

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static String getRealPathFromURI(Uri contentUri, Activity a) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = a.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();//from  w  w  w.  j av  a  2  s. co m
    return cursor.getString(column_index);
}

From source file:com.karura.framework.plugins.Capture.java

/**
 * Queries the media store to find out what the file path is for the Uri we supply
 * //  ww  w.  j a va  2  s.c o m
 * @param contentUri
 *            the Uri of the audio/image/video
 * @param context
 *            the current application context
 * @return the full path to the file
 */
@SuppressWarnings("deprecation")
public static String getRealPathFromURI(Uri contentUri, Activity context) {
    final String scheme = contentUri.getScheme();

    if (scheme == null) {
        return contentUri.toString();
    } else if (scheme.compareTo("content") == 0) {
        String[] proj = { DATA };
        Cursor cursor = context.managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else if (scheme.compareTo("file") == 0) {
        return contentUri.getPath();
    } else {
        return contentUri.toString();
    }
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *//*from   www.  ja va  2s  .  co m*/
public static String getRealPathFromURI(Activity activity, Uri contentUri) {

    String releaseNumber = Build.VERSION.RELEASE;

    if (releaseNumber != null) {
        /* ICS Version */
        if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
            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();
            }
            return strFileName;
        }
        /* GB Version */
        else if (releaseNumber.startsWith("2.3")) {
            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();
            }
            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;
}

From source file:ca.ualberta.cmput301.t03.inventory.AddItemView.java

/**
 * Used to get the path of the file selected in the gallery selector. This is called in the
 * onActivityResult handler./*from www.jav  a  2  s  . c o  m*/
 *
 * Code used:
 * http://stackoverflow.com/questions/27874038/how-to-make-intent-chooser-for-camera-or-gallery-application-in-android-like-wha
 * @param uri the uri returned by the gallery intent
 * @param activity this activity
 * @return
 */
public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaStore.MediaColumns.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

From source file:com.stoneapp.ourvlemoodle2.tasks.EventSync.java

private String getCalendarUriBase(Context context) {
    Activity act = (Activity) context;
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;/*from www .ja  v  a  2s. c o  m*/
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }

    return calendarUriBase;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *//*ww w .ja  va 2s .co  m*/
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;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *///from  w ww  .  j  a  v  a2s .c  o m
@SuppressWarnings("deprecation")
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;
}

From source file:com.oxgcp.photoList.PhotolistModule.java

@Kroll.method
public KrollDict getPhotos(Integer date, Integer limit) {
    Uri externalPhotosUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    // Uri internalPhotosUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
    Activity activity = this.getActivity();
    Gson gson = new Gson();

    String orderBy;/*from  ww  w .ja  va 2s  .co m*/
    if (date == null)
        date = 0;
    if (limit == null) {
        orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";
    } else {
        orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT " + limit;
    }

    String[] where = { date.toString() };

    HashMap<String, String> obj = new HashMap<String, String>();
    // HashMap<String, KrollDict> photos = new HashMap<String, KrollDict>();
    // KrollDict dict;

    // String[] projection = new String[]{
    //    MediaStore.Images.Media._ID,
    //    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
    //    MediaStore.Images.Media.DATE_TAKEN
    // };

    Cursor externalList = activity.managedQuery(externalPhotosUri, null,
            MediaStore.Images.ImageColumns.DATE_TAKEN + " > ? ", where, orderBy);
    // null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC WHERE " + MediaStore.Images.ImageColumns._ID + " > " + id + " LIMIT " + limit);
    // Cursor internalList = activity.managedQuery(internalPhotosUri, null, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

    // ArrayList<String> arrayList = new ArrayList<String>();
    KrollDict arrayList = new KrollDict(externalList.getCount());

    externalList.moveToFirst();
    // internalList.moveToFirst();

    Log.d("TiAPI", "externalList's count: " + externalList.getCount());
    if (externalList.getCount() > 0) {
        for (Integer i = 0; !externalList.isAfterLast(); i++) {

            obj.put("path", externalList.getString(externalList.getColumnIndex(MediaStore.MediaColumns.DATA)));
            obj.put("date", externalList
                    .getString(externalList.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN)));

            // dict = new KrollDict(obj);

            arrayList.put(i.toString(), new KrollDict(obj)); //add the item
            externalList.moveToNext();
        }
    }

    // Log.d("TiAPI", "internalList's count: " + internalList.getCount());
    // if (internalList.getCount() > 0) {
    //    while(!internalList.isAfterLast()) {
    //          arrayList.add(internalList.getString(internalList.getColumnIndex(MediaStore.MediaColumns.DATA))); //add the item
    //          internalList.moveToNext();
    //    }
    // }

    return arrayList;//gson.toJson(arrayList);
}

From source file:org.cryptsecure.Utility.java

/**
 * Convert the image URI to the direct file system path of the image so that
 * it can be loaded.//from w w  w  . j a v  a 2  s  .  c om
 * 
 * @param contentUri
 *            the content uri
 * @return the real path from uri
 */
@SuppressLint("NewApi")
// file
@SuppressWarnings("deprecation")
public static String getRealPathFromURI(Activity activity, Uri contentUri) {
    String returnPath = null;

    Log.d("communicator", "IMPORT getRealPathFromURI() contentUri=" + contentUri.toString());

    try {
        if (Build.VERSION.SDK_INT < 19) {
            // can post image
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = activity.managedQuery(contentUri, proj, // Which
                    // columns
                    // to
                    // return
                    null, // WHERE clause; which rows to return (all rows)
                    null, // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            returnPath = cursor.getString(column_index);
        } else if (Build.VERSION.SDK_INT > 19) {
            String[] projection = { MediaStore.Images.Media.DATA };
            String wholeID = DocumentsContract.getDocumentId(contentUri);
            String id = wholeID.split(":")[1];
            String sel = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, sel, new String[] { id }, null);
            int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            returnPath = cursor.getString(column_index).toString();
            cursor.close();
        } else {
            returnPath = contentUri.toString();
        }
    } catch (Exception e) {
    }

    Log.d("communicator", "IMPORT getRealPathFromURI() returnPath=" + returnPath);

    return returnPath;
}