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:Main.java

public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, 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)

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }//from  w w w .ja  v  a 2 s . c  o m
    }

    return imagePath;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String getAbsoluteImagePath(Activity context, Uri uri) {
    String imagePath = "";
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(uri, 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)

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            imagePath = cursor.getString(column_index);
        }//w w w  .  j a va2 s. c om
    }

    return imagePath;
}

From source file:Main.java

public static String getFileNameFromUri(Uri uri, Activity activity) {
    String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
    String fileName = null;/* ww  w . j  a  v a  2  s.c o  m*/
    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        fileName = c.getString(0);
    }
    return fileName;

}

From source file:Main.java

public static String getLatestImage(Activity context) {
    String latestImage = null;// ww  w. j a  va  2s .c  om
    String[] items = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    Cursor cursor = context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null, null,
            MediaStore.Images.Media._ID + " desc");

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            latestImage = cursor.getString(1);
            break;
        }
    }

    return latestImage;
}

From source file:Main.java

public static List<String> getAllNumbers(Activity callingActivity) {
    List<String> allNumbers = new ArrayList<String>();
    Cursor managedCursor = callingActivity.managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
    StringBuilder sb = new StringBuilder();
    sb.append("Call Details :");
    while (managedCursor.moveToNext()) {
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.getString(duration);
        String dir = null;/*w  ww . ja v  a  2 s  . com*/
        if (!allNumbers.contains(phNumber)) {
            allNumbers.add(phNumber);
        }

        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;

        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- "
                + callDayTime + " \nCall duration in sec :--- " + callDuration);
        sb.append("\n----------------------------------");
        // Log.i("getAllNumbers", sb.toString());
    }
    managedCursor.close();

    return allNumbers;
}

From source file:Main.java

public static File convertImageUriToFile(Uri imageUri, Activity activity) {
    Cursor cursor = null;//from www .j  av a 2 s  .  c o m
    try {
        String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.ORIENTATION };
        cursor = activity.managedQuery(imageUri, proj, null, null, null);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            String orientation = cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.todoroo.astrid.actfm.ActFmCameraModule.java

private static Bitmap bitmapFromUri(Activity activity, Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    String path;/*from   w ww . j a  v  a  2s  .c  om*/

    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index);
    } else {
        path = uri.getPath();
    }

    return AndroidUtilities.readScaledBitmap(path);
}

From source file:Main.java

public static File getFileFromUri(Uri uri, Activity activity) {
    String filePath = null;// w  w  w. j a  v a2 s.c  o  m
    String scheme = uri.getScheme();
    filePath = uri.getPath();
    if (filePath != null && scheme != null && scheme.equals("file")) {
        return new File(filePath);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DATA };
    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        filePath = c.getString(0);
    }
    if (filePath != null) {
        return new File(filePath);
    }
    return null;

}

From source file:Main.java

public static boolean haveCalendars(Activity ctx) {

    boolean exists = false;

    String[] projection = new String[] { "_id", "name" };
    Uri calendars = Uri.parse("content://calendar/calendars");

    // Calendar uri changes for 2.2
    Uri calendars_2_2 = Uri.parse("content://com.android.calendar/calendars");
    Cursor managedCursor = ctx.managedQuery(calendars, projection, null, null, null);

    if (managedCursor == null || managedCursor.getCount() == 0)
        managedCursor = ctx.managedQuery(calendars_2_2, projection, null, null, null);

    if (managedCursor != null && managedCursor.moveToFirst()) {
        String calName;/*from w  ww .  j  ava  2 s . com*/
        String calId;
        int nameColumn = managedCursor.getColumnIndex("name");
        int idColumn = managedCursor.getColumnIndex("_id");
        do {
            calName = managedCursor.getString(nameColumn);
            calId = managedCursor.getString(idColumn);
            exists = true;
        } while (managedCursor.moveToNext());

        managedCursor.close();
    }

    return exists;
}

From source file:Main.java

public static String extractFilenameFromUri(Uri uri, Activity activity) {

    String fileName = null;//from   w  w  w .ja  v a 2s .  com
    if (uri == null) {
        throw new IllegalArgumentException();
    }
    String scheme = uri.getScheme();
    String path = uri.getPath();
    if (path != null && scheme != null && scheme.equals("file")) {
        fileName = path.substring(path.lastIndexOf("/") + 1);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME /* col1 */ };

    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        fileName = c.getString(0);
    }
    return fileName;
}