List of usage examples for android.database Cursor getColumnIndexOrThrow
int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;
From source file:Main.java
/** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context./* w w w . j a v a 2 s . com*/ * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. * @author paulburke */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } catch (Exception e) { return null; } finally { if (cursor != null) cursor.close(); } return null; }
From source file:Main.java
public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) { String[] smsLogArray = new String[2]; Uri uri = Uri.parse("content://sms/inbox"); Cursor cur = cr.query(uri, null, null, null, null); FileOutputStream fOut = null; try {//from www. j a va 2 s . co m fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); } OutputStreamWriter osw = new OutputStreamWriter(fOut); while (cur.moveToNext()) { smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString(); smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString(); writeToOutputStreamArray(smsLogArray, osw); } try { osw.close(); } catch (IOException e) { e.printStackTrace(); } return internal; }
From source file:Main.java
static String convertUriToPath(Context context, Uri uri) { Log.v(TAG, "convertUriToPath : " + uri + " @" + context); String path = null;/*ww w . java2 s . co m*/ if (null != uri) { String scheme = uri.getScheme(); if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) { path = uri.getPath(); } else if (scheme.equals("http")) { path = uri.toString(); } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) { String[] projection = new String[] { MediaStore.MediaColumns.DATA }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) { throw new IllegalArgumentException("Given Uri could not be found in media store"); } int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); path = cursor.getString(pathIndex); } catch (SQLiteException e) { throw new IllegalArgumentException( "Given Uri is not formatted in a way so that it can be found in media store."); } finally { if (null != cursor) { cursor.close(); } } } else { throw new IllegalArgumentException("Given Uri scheme is not supported"); } } Log.v(TAG, "convertUriToPath : >" + path); return path; }
From source file:edu.mit.mobile.android.locast.data.CastMedia.java
/** * Returns the uri for the media. It will return, in order, the local uri if it's present, the * low-res uri (if {@link #shouldShowLowQuality(Context)} says so), or the full-res URI. * * @param context/* www . j a va2 s. c om*/ * @param c * @return null or a url pointing to the media */ public static Uri getMedia(Context context, Cursor c) { Uri media; final int mediaLocalCol = c.getColumnIndexOrThrow(_LOCAL_URI); final int mediaCol = c.getColumnIndexOrThrow(_MEDIA_URL); if (!c.isNull(mediaLocalCol)) { media = Uri.parse(c.getString(mediaLocalCol)); } else if (shouldShowLowQuality(context)) { final int mediaLowResCol = c.getColumnIndexOrThrow(_LOW_BITRATE_URL); media = Uri.parse(c.getString(mediaLowResCol)); } else if (!c.isNull(mediaCol)) { media = Uri.parse(c.getString(mediaCol)); } else { media = null; } return media; }
From source file:Main.java
public static ArrayList<String> getAllShownImagesPath(Activity activity) { Uri uri;/*w ww. ja v a 2s . c o m*/ Cursor cursor; int column_index_data, column_index_folder_name; ArrayList<String> listOfAllImages = new ArrayList<String>(); String absolutePathOfImage = null; uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; String[] projection = { MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME }; cursor = activity.getContentResolver().query(uri, projection, null, null, null); column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); while (cursor.moveToNext()) { absolutePathOfImage = cursor.getString(column_index_data); listOfAllImages.add(absolutePathOfImage); } return listOfAllImages; }
From source file:com.fede.Utilities.GeneralUtils.java
public static String[] getMissedCalls(Context context) { Long lastFlushed = PrefUtils.getLastFlushedCalls(context); java.text.DateFormat timeForm = android.text.format.DateFormat.getTimeFormat(context); Long yesterday = get24HoursAgoTime(); if (lastFlushed == 0 || lastFlushed < yesterday) { lastFlushed = yesterday; // not later than 24 hours ago }/*w w w .j av a2s . c om*/ String where = android.provider.CallLog.Calls.TYPE + " = " + android.provider.CallLog.Calls.MISSED_TYPE + " and " + android.provider.CallLog.Calls.DATE + " > " + lastFlushed; Cursor c = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, where, null, android.provider.CallLog.Calls.DATE + " DESC"); int nameIdx = c.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME); int numbIdx = c.getColumnIndexOrThrow(CallLog.Calls.NUMBER); int dateIdx = c.getColumnIndexOrThrow(CallLog.Calls.DATE); String[] result = new String[c.getCount()]; if (c.moveToFirst()) do { String name = c.getString(nameIdx); name = (name != null) ? name : ""; String number = c.getString(numbIdx); String when = timeForm.format(c.getLong(dateIdx)); result[c.getPosition()] = String.format(context.getString(R.string.flushed_calls), name, number, when); } while (c.moveToNext()); c.close(); PrefUtils.setLastFlushedCalls(context); return result; }
From source file:Main.java
/** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context//w w w . jav a 2 s. c o m * The context. * @param uri * The Uri to query. * @param selection * (Optional) Filter used in the query. * @param selectionArgs * (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. * @author paulburke */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; }
From source file:Main.java
public static Uri getAllContacts(ContentResolver cr, Uri internal, Context context, String timeStamp) { String[] contactsArray = new String[2]; Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; Cursor cur = cr.query(contactsUri, null, null, null, null); FileOutputStream fOut = null; try {//from w w w.j a v a2s . c o m fOut = context.openFileOutput("contacts_" + timeStamp + ".txt", Context.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); } OutputStreamWriter osw = new OutputStreamWriter(fOut); while (cur.moveToNext()) { contactsArray[0] = cur .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) .toString(); contactsArray[1] = cur .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); writeToOutputStreamArray(contactsArray, osw); } try { osw.close(); } catch (IOException e) { e.printStackTrace(); } return internal; }
From source file:Main.java
public static LinkedList<Pair<Integer, String>> retrieveIntegerStringPairFromCursor(Cursor cursor, String integerColumnName, String stringColumnName) { LinkedList<Pair<Integer, String>> result = new LinkedList<Pair<Integer, String>>(); if (null == cursor || 0 == cursor.getCount()) { return result; }//from ww w . j a v a2 s . c om try { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Integer integerVal = cursor.getInt(cursor.getColumnIndex(integerColumnName)); String stringVal = cursor.getString(cursor.getColumnIndexOrThrow(stringColumnName)); result.add(new Pair(integerVal, stringVal)); } } catch (Exception e) { //do nothing. } finally { cursor.close(); } return result; }
From source file:com.fede.Utilities.GeneralUtils.java
public static String[] getContactNumbers(String contact, Context c) throws NameNotFoundException { // Find a contact using a partial name match Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, contact); Cursor idCursor = c.getContentResolver().query(lookupUri, null, null, null, null); String id = null;// ww w . jav a2s . co m if (idCursor.moveToFirst()) { // I try with the first record int idIdx = idCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID); id = idCursor.getString(idIdx); } idCursor.close(); if (id == null) throw new NameNotFoundException(c.getString(R.string.name_not_found)); // Return all the contact details of type PHONE for the contact we found String where = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'"; Cursor dataCursor = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, null, null); // Use the convenience properties to get the index of the columns int nameIdx = dataCursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME); int phoneIdx = dataCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER); String[] result = new String[dataCursor.getCount()]; if (dataCursor.getCount() == 0) throw new NameNotFoundException(c.getString(R.string.name_not_valid_numbers)); if (dataCursor.moveToFirst()) do { // Extract the name. String name = dataCursor.getString(nameIdx); // Extract the phone number. String number = dataCursor.getString(phoneIdx); int position = dataCursor.getPosition(); if (position == 0) // I put the name only in the first record to save space result[position] = name + " (" + number + ")"; else result[position] = " (" + number + ")"; } while (dataCursor.moveToNext()); dataCursor.close(); return result; }