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

/**
 * This method to get real path from uri.
 * /* ww  w.  java 2s .c  o m*/
 * @param context
 * @param contentURI
 * @return imagepath
 */
public static String getRealPathFromURI(Context context, Uri contentURI) {
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;
    if ("file".equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if ("content".equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;
        try {/*from w ww .  j a  v  a 2s  . c  o m*/
            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);
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets a contact number and then displays the name of contact in the DP
 *
 * @param context       context of the activity from which it was called.
 * @param contactNumber a string representation of the contact number
 * @return returns the number if no contact exist.
 *///from   www  .j a va2  s . c  o  m
public static String getContactName(Context context, String contactNumber) {
    String displayName = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (cur != null && cur.moveToFirst()) {
        displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    } else {
        displayName = contactNumber;
    }

    if (!cur.isClosed()) {
        cur.close();
    }

    return displayName;
}

From source file:Main.java

/**
 * method used to lookup the id of a contact photo id
 *
 * @param context//from w  ww  .  j  a  v  a 2  s  . c  om
 *            a context object used to get a content resolver
 * @param phoneNumber
 *            the phone number of the contact
 *
 * @return the id of the contact
 */
public static long lookupPhotoId(Context context, String phoneNumber) {

    long mPhotoId = -1;

    Uri mLookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] mProjection = new String[2];
    mProjection[0] = PhoneLookup._ID;
    mProjection[1] = PhoneLookup.PHOTO_ID;

    Cursor mCursor = context.getContentResolver().query(mLookupUri, mProjection, null, null, null);

    if (mCursor.getCount() > 0) {
        mCursor.moveToFirst();

        mPhotoId = mCursor.getLong(mCursor.getColumnIndex(PhoneLookup._ID));

        mCursor.close();
    }

    return mPhotoId;
}

From source file:Main.java

public static void CheckDwnloadStatus(DownloadManager downloadManager, Activity activity, long id) {

    // TODO Auto-generated method stub
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(id);//w  w w. j ava  2s .  co m
    Cursor cursor = downloadManager.query(query);
    if (cursor.moveToFirst()) {
        int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(columnReason);

        switch (status) {
        case DownloadManager.STATUS_FAILED:
            String failedReason = "";
            switch (reason) {
            case DownloadManager.ERROR_CANNOT_RESUME:
                failedReason = "ERROR_CANNOT_RESUME";
                break;
            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                failedReason = "ERROR_DEVICE_NOT_FOUND";
                break;
            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                failedReason = "ERROR_FILE_ALREADY_EXISTS";
                break;
            case DownloadManager.ERROR_FILE_ERROR:
                failedReason = "ERROR_FILE_ERROR";
                break;
            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                failedReason = "ERROR_HTTP_DATA_ERROR";
                break;
            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                failedReason = "ERROR_INSUFFICIENT_SPACE";
                break;
            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                failedReason = "ERROR_TOO_MANY_REDIRECTS";
                break;
            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                break;
            case DownloadManager.ERROR_UNKNOWN:
                failedReason = "ERROR_UNKNOWN";
                break;
            default:
                failedReason = "unknown reason";
                break;
            }

            Toast.makeText(activity, "FAILED: " + failedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PAUSED:
            String pausedReason = "";

            switch (reason) {
            case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                pausedReason = "PAUSED_QUEUED_FOR_WIFI";
                break;
            case DownloadManager.PAUSED_UNKNOWN:
                pausedReason = "PAUSED_UNKNOWN";
                break;
            case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                pausedReason = "PAUSED_WAITING_FOR_NETWORK";
                break;
            case DownloadManager.PAUSED_WAITING_TO_RETRY:
                pausedReason = "PAUSED_WAITING_TO_RETRY";
                break;
            }

            Toast.makeText(activity, "PAUSED: " + pausedReason, Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PENDING:
            Toast.makeText(activity, "PENDING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_RUNNING:
            Toast.makeText(activity, "RUNNING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_SUCCESSFUL:

            Toast.makeText(activity, "SUCCESSFUL", Toast.LENGTH_LONG).show();
            break;
        }
    }
}

From source file:Main.java

public static String getSmsText(Context context, String msgId) {
    String result = null;/*from  w  ww.  jav a 2 s.  co m*/
    try {
        Cursor c = context.getContentResolver().query(Uri.parse("content://sms/inbox"),
                new String[] { "body", }, "_id = ?", new String[] { msgId, }, null);
        if (c.moveToFirst()) {
            result = c.getString(0);
        }
        c.close();
    } catch (Throwable t) {
        LOGE("getSmsText: " + t.getMessage());
        t.printStackTrace();
        result = null;
    }
    return result;
}

From source file:Main.java

public static String resolveFileName(Uri uri, Activity activity) {
    String filename = null;/*  w  w w .  j  a  va 2 s .c  o  m*/
    String uriString = uri.toString();

    if (uriString.startsWith("content://")) {
        Cursor cursor = null;
        try {
            cursor = activity.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String mimeType = MimeTypeMap.getSingleton()
                        .getExtensionFromMimeType(activity.getContentResolver().getType(uri));
                filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

                if (mimeType != null && !filename.endsWith("." + mimeType)) {
                    filename += "." + mimeType;
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uriString.startsWith("file://")) {
        filename = (new File(uriString)).getName();
    }
    return filename;
}

From source file:com.codebutler.farebot.key.CardKeys.java

public static CardKeys forTagId(byte[] tagId) throws Exception {
    String tagIdString = Utils.getHexString(tagId);
    FareBotApplication app = FareBotApplication.getInstance();
    Cursor cursor = app.getContentResolver()
            .query(Uri.withAppendedPath(CardKeyProvider.CONTENT_URI, tagIdString), null, null, null, null);
    if (cursor.moveToFirst()) {
        return CardKeys.fromCursor(cursor);
    } else {//  w w w.  j a  v a  2s  . com
        return null;
    }
}

From source file:Main.java

/**
 * Gets the Uri to a specific audio file
 * @param filePath The path of the file that we are looking up
 * @param contentResolver The content resolver that is used to perform the query
 * @return The Uri of the sound file/* w  w w.j  a va 2s.  com*/
 */
private static Uri getAudioUriFromFilePath(String filePath, ContentResolver contentResolver) {
    long audioId;
    Uri uri = MediaStore.Audio.Media.getContentUri("external");
    String[] projection = { BaseColumns._ID };
    Cursor cursor = contentResolver.query(uri, projection, MediaStore.MediaColumns.DATA + " LIKE ?",
            new String[] { filePath }, null);

    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        audioId = cursor.getLong(columnIndex);
        cursor.close();
        return Uri.parse(uri.toString() + "/" + audioId);
    }
    return null;
}

From source file:Main.java

public static Long getOrCreateThreadId(Context context, String phone) {
    try {//  w  ww.j a  v  a 2s  .c o m
        Uri threadIdUri = Uri.parse("content://mms-sms/threadID");
        Uri.Builder builder = threadIdUri.buildUpon();
        String[] recipients = { phone };
        for (String recipient : recipients) {
            builder.appendQueryParameter("recipient", recipient);
        }
        Uri uri = builder.build();
        Long threadId = 0L;
        Cursor cursor = context.getContentResolver().query(uri, new String[] { "_id" }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    threadId = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
            return threadId;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1L;
}