Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

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

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:com.example.facebook_photo.Utility.java

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }//ww w.  ja v  a 2  s. co  m

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();

    return orientation;
}

From source file:Main.java

public static String getPersonNameFromNumber(Context context, String box, String address) {
    if (address == null) {
        return "unknown";
    }/* ww w  .j  a v a 2 s . c  o m*/
    if (!box.equalsIgnoreCase("draft")) {
        Cursor cursor = context.getContentResolver().query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)),
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();
                    String name = cursor.getString(0);
                    return name;
                }
            } finally {
                cursor.close();
            }
        }
    }
    if (address != null) {
        return PhoneNumberUtils.formatNumber(address);
    }
    return "unknown";
}

From source file:Main.java

public static String[] getTrailers(Cursor data) {
    List<String> lista = new ArrayList<String>();
    lista.add("Select Trailer");

    data.moveToFirst();//from ww  w.jav a  2s  .  c om
    if (!data.getString(COL_TRAILER_KEY).equalsIgnoreCase("No Trailer Available")) {
        //loop through cursor to get all trailers
        for (int i = 1; i < data.getCount() + 1; i++) {
            if (!lista.contains(data.getString(COL_TRAILER_KEY))) {
                lista.add(data.getString(COL_TRAILER_KEY));
                data.moveToNext();
            } else {
                data.moveToNext();
            }
        }
    }

    String[] ltrailer;
    if (lista.size() < 1) {
        ltrailer = new String[1];
        ltrailer[0] = "No Trailer Available";
    } else {
        ltrailer = lista.toArray(new String[lista.size()]);
    }

    return ltrailer;
}

From source file:com.csipsimple.utils.SipProfileJson.java

public static JSONObject serializeSipProfile(SipProfile profile, DBAdapter db) {
    JSONObject jsonProfile = serializeBaseSipProfile(profile);
    JSONArray jsonFilters = new JSONArray();

    Cursor c = db.getFiltersForAccount(profile.id);
    int numRows = c.getCount();
    c.moveToFirst();/*w  w  w.  j  av  a 2s  .  c o m*/
    for (int i = 0; i < numRows; ++i) {
        Filter f = new Filter();
        f.createFromDb(c);

        try {
            jsonFilters.put(i, serializeBaseFilter(f));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add fitler", e);
            e.printStackTrace();
        }
        c.moveToNext();
    }
    c.close();

    try {
        jsonProfile.put(FILTER_KEY, jsonFilters);
    } catch (JSONException e) {
        Log.e(THIS_FILE, "Impossible to add fitlers", e);
    }

    return jsonProfile;
}

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*/
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:com.maskyn.fileeditorpro.util.AccessStorageApi.java

public static String getName(Context context, Uri uri) {

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    String fileName = "";
    try {/*from  www.java2 s.  c  om*/
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            fileName = uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Images.Media.DISPLAY_NAME };
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception ex) {
        return "";
    }
    return fileName;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *//*w  w w  .  j a v  a  2  s . c  o 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  ww w .ja  va  2  s  .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:sg.macbuntu.android.pushcontacts.SmsReceiver.java

private static String getNameFromPhoneNumber(Context context, String phone) {
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, phone),
            new String[] { Contacts.Phones.NAME }, null, null, null);
    if (cursor != null) {
        try {/* ww w .  ja v  a  2 s .  c  o  m*/
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                Log.e("PUSH_CONTACTS", "Pushed name: " + name);
                return name;
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:net.ccghe.emocha.model.DBAdapter.java

private static String getFirst(String column, String filter, String table, int columnId) {
    String result = null;//w  w  w. jav  a  2 s.c o m
    Cursor c = sDB.query(table, new String[] { column }, filter, null, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        result = c.getString(columnId);
    }
    c.close();
    return result;
}