Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

In this page you can find the example usage for android.database Cursor moveToFirst.

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;
        try {/*  w w  w.  ja va  2  s  .  com*/
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Return file size from Uri/*from w  ww.  ja  v  a  2s.  c o m*/
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:Main.java

public static Cursor getAllCallLogAboutACaller(Context context, String callerNumber) {
    // cancello la chiamata in uscita se nelle preferenze ? settata tale opzione
    Uri delUri = Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, "");
    Cursor cursor = context.getContentResolver().query(delUri, null,
            android.provider.CallLog.Calls.NUMBER + "=?", new String[] { "404" }, null);
    try {/* w ww.  j  av  a  2s. c  o m*/
        boolean moveToFirst = cursor.moveToFirst();
        Log.i("MOVETOFIRST", "moveToFirst=" + moveToFirst);
        do {
            int numberColumn = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
            String callerPhoneNumber = cursor.getString(numberColumn);
            Log.i(TAG, "numero : " + callerPhoneNumber);
        } while (cursor.moveToNext());

    } catch (Exception e) {
        Log.e(TAG, "Problem moving to first entry", e);
    }
    return cursor;
}

From source file:Main.java

/**
 * Return file display name from Uri/*from   w  w  w . ja  v a2 s.  co m*/
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}

From source file:Main.java

public static String getVideoPath(Activity activity, Uri uri) {
    String[] projection = { MediaStore.Video.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    activity.startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

From source file:Main.java

public static String getPath(Activity activity, Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    activity.startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

From source file:Main.java

public static String getLatestCameraPicture(Context context) {

    if (!existSDCard()) {
        return null;
    }/*from   w w  w . j  a v a  2  s.com*/

    String[] projection = new String[] { MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images.ImageColumns.MIME_TYPE };
    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
    if (cursor.moveToFirst()) {
        String path = cursor.getString(1);
        return path;
    }
    return null;
}

From source file:Main.java

/**
 * Utils to get File path/*from w w  w.  ja  v a2  s.  c  o m*/
 * 
 * @param uri
 * @return
 */
public static String getPath(Context context, Uri uri) {
    String scheme = uri.getScheme();
    String s = null;
    if (scheme.equals("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
        cursor.moveToFirst();
        s = cursor.getString(columnIndex);
    } else if (scheme.equals("file")) {
        s = uri.getPath();
    }
    // Log.d("ActionManager", "URI:" + uri + " - S:" + s);
    return s;
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 * //ww  w  .  j a  v a 2  s.c o m
 * @param context
 * @param uri
 * @return the file path or null
 */
public static String getRealFilePath(final Context context, final Uri uri) {

    if (null == uri)
        return null;

    final String scheme = uri.getScheme();
    String data = null;

    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static String geVideotName(Activity activity, Uri uri) throws Exception {

    String[] projection = { MediaStore.Video.Media.DATA };
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    activity.startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    Uri filePathUri = Uri.parse(cursor.getString(column_index));
    String file_name = filePathUri.getLastPathSegment().toString();
    return file_name;
}