List of usage examples for android.database Cursor getColumnIndex
int getColumnIndex(String columnName);
From source file:Main.java
public static Uri pathToContentUri(Context context, String imagePath) { Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[] { "_id" }, "_data=? ", new String[] { imagePath }, null); if (cursor != null && cursor.moveToFirst()) { int imageFile1 = cursor.getInt(cursor.getColumnIndex("_id")); Uri values1 = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(values1, "" + imageFile1); } else {// ww w .j a va 2 s .c o m File imageFile = new File(imagePath); if (imageFile.exists()) { ContentValues values = new ContentValues(); values.put("_data", imagePath); Uri baseUri = Media.EXTERNAL_CONTENT_URI; return context.getContentResolver().insert(baseUri, values); } else { return null; } } }
From source file:Main.java
/** * Try to return the absolute file path from the given Uri * /*from w w w .j a va 2s.co 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 int getLastImageId(Context context) { final String[] imageColumns = { Images.Media._ID }; final String imageOrderBy = Images.Media._ID + " DESC"; Cursor cursor = context.getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);/*from www .ja v a2s.c o m*/ if (cursor == null) return 0; int id = 0; if (cursor.moveToFirst()) { id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID)); } cursor.close(); return id; }
From source file:Main.java
/** * Get video's duration from {@link ContentProvider} * * @param context/* w w w. j a v a2s . c o m*/ * @param uri must has {@link Uri#getScheme()} equals * {@link ContentResolver#SCHEME_CONTENT} * @return Duration of video, in milliseconds. */ public static long getDuration(Context context, Uri uri) { long duration = 0L; Cursor cursor = MediaStore.Video.query(context.getContentResolver(), uri, new String[] { MediaStore.Video.VideoColumns.DURATION }); if (cursor != null) { cursor.moveToFirst(); duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION)); cursor.close(); } return duration; }
From source file:Main.java
/** * Using code from this link: http://hmkcode.com/android-display-selected-image-and-its-real-path/ * This method will return the absolute path Android.net.Uri. * NOTE!!! THIS DOES NOT SUPPORT API 10 OR BELOW!!! IF YOU NEED TO WORK WITH THAT, CHECK LINK ABOVE * @param context Context//from w ww . j av a 2s . c o m * @param uri Uri to check * @return String for the absolute path */ public static String getAbsolutePath(Context context, android.net.Uri uri) { if (Build.VERSION.SDK_INT >= 19) { try { String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the array String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } catch (Exception e) { e.printStackTrace(); return null; } } else { try { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader(context, uri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } catch (Exception e) { e.printStackTrace(); return null; } } }
From source file:Main.java
private static String getRealPathFromURI(Context context, Uri contentURI) { String result;//ww w. j ava 2 s.co m Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null); if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close(); } return result; }
From source file:Main.java
public static synchronized void deleteCalllogByPhone(Context context, String number) { if (TextUtils.isEmpty(number)) return;//from w ww .j ava 2s. c o m try { Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, Calls.NUMBER + "=?", new String[] { number }, Calls.DATE + " desc"); if (cursor != null) { if (cursor.moveToNext()) { long _id = cursor.getLong(cursor.getColumnIndex(Calls._ID)); context.getContentResolver().delete(Calls.CONTENT_URI, Calls._ID + "=?", new String[] { String.valueOf(_id) }); } cursor.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Query the server app to get the file's display name. *//*from w w w . j a va 2s .c om*/ public static String getUriFileName(Context context, Uri uri) { if (uri == null) { return null; } if (uri.getScheme().equals("file")) { return uri.getLastPathSegment(); } if (uri.getScheme().equals("content")) { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); //Get the column index of the data in the Cursor, //move to the first row in the Cursor, get the data, and display it. int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE); if (name_index < 0) { return null; } returnCursor.moveToFirst(); //return returnCursor.getLong(size_index) return returnCursor.getString(name_index); } return null; }
From source file:edu.mit.mobile.android.locast.data.Itinerary.java
public static List<GeoPoint> getPath(Cursor c) { final ArrayList<GeoPoint> path = new ArrayList<GeoPoint>(); final String encodedPath = c.getString(c.getColumnIndex(_PATH)); if (encodedPath == null) { return new ArrayList<GeoPoint>(); }/*from w ww .j av a2 s. c om*/ int prevI = 0; int i = 0; while (i != -1) { i = encodedPath.indexOf(',', i + 1); // handle the case for empty paths if (i == -1) { break; } final int lat = Integer.parseInt(encodedPath.substring(prevI, i)); prevI = i + 1; i = encodedPath.indexOf(',', prevI); int end = i; if (i == -1) { end = encodedPath.length(); } final int lon = Integer.parseInt(encodedPath.substring(prevI, end)); path.add(new GeoPoint(lat, lon)); prevI = i + 1; } return path; }
From source file:Main.java
/** * Finding contact name by telephone number. * * @param context Context//from w w w . j a v a2 s . co m * @param number Telephone number * @return Contact display name */ public static String getContactName(Context context, String number) { Log.d(TAG, "Searching contact with number: " + number); /* define the columns I want the query to return */ String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }; /* encode the phone number and build the filter URI */ Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); /* query time */ Log.d(TAG, "Sending query"); Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null); String name = null; if (cursor.moveToFirst()) { /* Get values from contacts database: */ name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); Log.d(TAG, "Contact found for number: " + number + ". The name is: " + name); } else { Log.d(TAG, "Contact not found for number: " + number); } return name; }