@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { String originalImagePath = ""; CursorLoader cursorLoader = (CursorLoader) loader; boolean catchedExpetion = false; if (data == null) { originalImagePath = cursorLoader.getUri().getPath(); } else { int columnIndex = data.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); data.moveToFirst(); try { originalImagePath = data.getString(columnIndex); } catch (CursorIndexOutOfBoundsException e) { catchedExpetion = true; } } if (catchedExpetion || (data == null && originalImagePath.equals(""))) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); return; } copyImageToCatroid(originalImagePath); }
// And to convert the image URI to the direct file system path of the image file private String getRealPathFromURI(Uri contentUri) { String[] proj = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
private void resetCursorLoaderUri() { Loader<Cursor> loader = getLoaderManager().getLoader(LOADER_ID_LIST); if (loader == null) { return; } CursorLoader cl = (CursorLoader) loader; cl.setUri(POIs.createUriSorted(mLocation)); loader.forceLoad(); }
public String getRealPathFromURI(Uri contentUri) { String[] proj = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null); Cursor cursor = loader.loadInBackground(); if (cursor != null && cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); String result = cursor.getString(column_index); cursor.close(); return result; } return null; }
@Override public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { if (mObjects == null) return; boolean loadMore = /* maybe add a padding */ firstVisible + visibleCount >= mObjects.getTotalQueried(); if (loadMore) { mLoader.cancelLoad(); if (getActivity() instanceof Filterable) { Filterable context = (Filterable) getActivity(); if (context != null) { List<String> filterTypes = new ArrayList<String>(); for (int x = 0; x < context.getFilterTypes().length; x++) { if (context.getFilterCheckboxes()[x]) { Log.w(TAG, "adding " + context.getFilterTypes()[x]); filterTypes.add(context.getFilterTypes()[x]); } } mLoader = mObjects.queryLaterObjects( getActivity(), mFeedUri, totalCount, filterTypes.toArray(new String[filterTypes.size()])); } } else { mLoader = mObjects.queryLaterObjects(getActivity(), mFeedUri, totalCount, null); } } }
/** * 把uri转为File对象 * * @param context * @param uri * @return */ public static File uri2File(Context context, Uri uri) { // 而managedquery在api 11 被弃用,所以要转为使用CursorLoader,并使用loadInBackground来返回 Cursor cursor = null; try { String[] projection = {MediaStore.Images.Media.DATA}; CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null); cursor = loader.loadInBackground(); if (null == cursor) { return null; } int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return new File(cursor.getString(column_index)); } catch (Exception ex) { Logger.e(TAG, ex); } finally { if (cursor != null) { cursor.close(); } } return null; }
@Override public void onDestroy() { super.onDestroy(); if (mLoader != null) { mLoader.cancelLoad(); } // the mObjects field is accessed by the background loader synchronized (this) { if (mObjects != null) { ((ObjectListCursorAdapter) mObjects).closeCursor(); } } }
@Test public void testGetters() { Uri uri = Uri.parse("http://robolectric.org"); String[] projection = new String[] {"_id", "TestColumn"}; String selection = "_id = ?"; String[] selectionArgs = new String[] {"5"}; String sortOrder = "_id"; CursorLoader cursorLoader = new CursorLoader( RuntimeEnvironment.application, uri, projection, selection, selectionArgs, sortOrder); assertThat(cursorLoader.getUri()).isEqualTo(uri); assertThat(cursorLoader.getProjection()).isEqualTo(projection); assertThat(cursorLoader.getSelection()).isEqualTo(selection); assertThat(cursorLoader.getSelectionArgs()).isEqualTo(selectionArgs); assertThat(cursorLoader.getSortOrder()).isEqualTo(sortOrder); }