List of usage examples for android.database Cursor close
void close();
From source file:com.radadev.xkcd.Comics.java
public static void downloadComic(Integer comicNumber, Context context) throws FileNotFoundException, IOException { ComicDbAdapter dbAdapter = new ComicDbAdapter(context); dbAdapter.open();//from w w w.j a v a 2s . c om try { dbAdapter.updateComic(comicNumber); File file = new File(getSdDir(context), comicNumber.toString() + ".png"); if (file.length() <= 0) { Cursor cursor = dbAdapter.fetchComic(comicNumber); String url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE)); if (url == null || url.length() == 0) { dbAdapter.updateComic(comicNumber); cursor.close(); cursor = dbAdapter.fetchComic(comicNumber); url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE)); } cursor.close(); Bitmap bitmap = Comics.downloadBitmap(url); FileOutputStream fileStream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, fileStream); bitmap.recycle(); fileStream.close(); } } finally { dbAdapter.close(); } }
From source file:ca.mudar.parkcatcher.utils.ParserUtils.java
/** * Query and return the {@link SyncColumns#UPDATED} time for the requested * {@link Uri}. Expects the {@link Uri} to reference a single item. *//*from w ww. ja v a 2 s . com*/ public static long queryItemUpdated(Uri uri, ContentResolver resolver) { final String[] projection = { SyncColumns.UPDATED }; final Cursor cursor = resolver.query(uri, projection, null, null, null); try { if (cursor.moveToFirst()) { return cursor.getLong(0); } else { return ParkingContract.UPDATED_NEVER; } } finally { cursor.close(); } }
From source file:Main.java
public static String getFilePathByContentResolver(Context context, Uri uri) { if (null == uri) { return null; }/* w w w. ja v a 2 s.c o m*/ Cursor c = context.getContentResolver().query(uri, null, null, null, null); String filePath = null; if (null == c) { throw new IllegalArgumentException("Query on " + uri + " returns null result."); } try { if ((c.getCount() != 1) || !c.moveToFirst()) { } else { filePath = c.getString(c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)); } } finally { c.close(); } return filePath; }
From source file:Main.java
public static File getRealFileFromURI(Context context, Uri contentUri) { Cursor cursor = null; try {/*from w w w . j av a2 s. c o 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(); String path = cursor.getString(column_index); return new File(path); } finally { if (cursor != null) { cursor.close(); } } }
From source file:Main.java
public static Map<String, Integer> getCity(SQLiteDatabase db, String tableName, int dqx_dqxx01, boolean municipalities) { Map<String, Integer> cityMap = new LinkedHashMap<String, Integer>(); Cursor cursor = db.query(tableName, new String[] { "DQXX02", "DQXX01" }, "DQX_DQXX01=?", new String[] { "" + dqx_dqxx01 }, null, null, "DQXX01 ASC"); if (cursor != null) { if (municipalities) { cursor.moveToNext();/*ww w . java 2s.c o m*/ } while (cursor.moveToNext()) { cityMap.put(cursor.getString(0), cursor.getInt(1)); } } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return cityMap; }
From source file:Main.java
protected static String getImagePath(Uri imageUri, Context context) { String scheme = imageUri.getScheme(); if (scheme.equals("file")) return imageUri.getPath(); Cursor cursor = null; try {/* w w w. j a va 2 s . co m*/ String[] proj = { MediaStore.Images.Media.DATA }; cursor = context.getContentResolver().query(imageUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } finally { if (cursor != null) cursor.close(); } }
From source file:Main.java
/** * Get the real file path from URI registered in media store * @param contentUri URI registered in media store *//* 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
private static InputStream openPhoto(final Context context, final Uri contactUri) { Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); Cursor cursor = context.getContentResolver().query(photoUri, new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null); if (cursor == null) { return null; }//from w w w. j av a2 s. c o m try { if (cursor.moveToFirst()) { byte[] data = cursor.getBlob(0); if (data != null) { return new ByteArrayInputStream(data); } } } finally { cursor.close(); } return null; }
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. j ava 2 s .com @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:Main.java
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; String column = MediaStore.Images.Media.DATA; String[] projection = { column }; try {//from w ww .j a va 2s .c o m cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; }