List of usage examples for android.database Cursor moveToFirst
boolean moveToFirst();
From source file:Main.java
/** * Returns the Id for an artist.// w w w . j ava2s .co m * * @param context The {@link Context} to use. * @param name The name of the artist. * @return The ID for an artist. */ public static final long getIdForArtist(final Context context, final String name) { Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, new String[] { BaseColumns._ID }, ArtistColumns.ARTIST + "=?", new String[] { name }, ArtistColumns.ARTIST); int id = -1; if (cursor != null) { cursor.moveToFirst(); if (!cursor.isAfterLast()) { id = cursor.getInt(0); } cursor.close(); cursor = null; } return id; }
From source file:Main.java
private static int getImageRotationAngle(Uri imageUri, ContentResolver contentResolver) throws IOException { int angle = 0; Cursor cursor = contentResolver.query(imageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);/* w w w . j a v a 2s. c o m*/ if (cursor != null) { if (cursor.getCount() == 1) { cursor.moveToFirst(); angle = cursor.getInt(0); } cursor.close(); } else { ExifInterface exif = new ExifInterface(imageUri.getPath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: angle = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: angle = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: angle = 90; break; default: break; } } return angle; }
From source file:Main.java
public static String getPath(Context context, Uri uri) { if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor; try {/* w w w.ja va2s . com*/ cursor = context.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndex("_data"); if (column_index != -1 && cursor.moveToFirst()) { String path = cursor.getString(column_index); if (path == null) { path = getNewTemporaryFilePath(context, uri); } return path; } else { return getNewTemporaryFilePath(context, uri); } } catch (Exception e) { return getNewTemporaryFilePath(context, uri); } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; }
From source file:Main.java
/** * Returns The ID for a playlist./*from ww w . java 2 s. com*/ * * @param context The {@link Context} to use. * @param name The name of the playlist. * @return The ID for a playlist. */ public static final long getIdForPlaylist(final Context context, final String name) { Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[] { BaseColumns._ID }, PlaylistsColumns.NAME + "=?", new String[] { name }, PlaylistsColumns.NAME); int id = -1; if (cursor != null) { cursor.moveToFirst(); if (!cursor.isAfterLast()) { id = cursor.getInt(0); } cursor.close(); cursor = null; } return id; }
From source file:Main.java
public static String getRealPathFromURI(Context context, Uri contentUri) { Cursor cursor = null; try {//w w w . ja v a 2 s . co m String[] proj = { MediaStore.Images.Media.DATA }; cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } catch (NullPointerException ex) { return ""; } finally { if (cursor != null) { cursor.close(); } } }
From source file:Main.java
public static String checkNull(Context context, int lastImageId, File fileCapture) { final String[] imageColumns = { Images.Media._ID, Images.Media.DATA }; final String imageOrderBy = Images.Media._ID + " DESC"; final String imageWhere = Images.Media._ID + ">?"; final String[] imageArguments = { Integer.toString(lastImageId) }; ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere, imageArguments, imageOrderBy); if (cursor == null) return null; String newpath = null;/*from w ww.j a va 2s . c om*/ if (cursor.getCount() >= 2) { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID)); String data = cursor.getString(cursor.getColumnIndex(Images.Media.DATA)); if (data.equals(fileCapture.getPath())) { int rows = contentResolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media._ID + "=?", new String[] { Long.toString(id) }); boolean ok = fileCapture.delete(); } else { newpath = data; } } } else { newpath = fileCapture.getPath(); Log.e("MediaUtils", "Not found duplicate."); } cursor.close(); return newpath; }
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 a2 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
/** * Looks up a contacts display name by contact id - if not found, the * address (phone number) will be formatted and returned instead. *///from ww w .j a v a 2 s . co m public static String getPersonName(Context context, String id, String address) { // Check for id, if null return the formatting phone number as the name if (id == null || "".equals(id.trim())) { if (address != null && !"".equals(address.trim())) { return PhoneNumberUtils.formatNumber(address); } else { return null; } } Cursor cursor = context.getContentResolver().query(Uri.withAppendedPath(Contacts.CONTENT_URI, id), new String[] { Contacts.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 null; }
From source file:Main.java
public static byte[] getPersonPhoto(Context context, String id) { if (id == null) return null; byte photo[] = null; Cursor cursor = context.getContentResolver().query(Uri.withAppendedPath(Contacts.Photos.CONTENT_URI, id), new String[] { PhotosColumns.DATA }, null, null, null); if (cursor != null) { try {/*from w ww . jav a 2s .c o m*/ if (cursor.getCount() > 0) { cursor.moveToFirst(); photo = cursor.getBlob(0); if (photo != null) { return photo; // Log.v("Found photo for person: " + id); // bitmap = BitmapFactory.decodeStream(new // ByteArrayInputStream(photo)); } } } finally { cursor.close(); } } return photo; }
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 w w.j a v a2 s .c om 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; }