Java Code Examples for android.support.v4.content.CursorLoader#loadInBackground()

The following examples show how to use android.support.v4.content.CursorLoader#loadInBackground() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AgentWebUtils.java    From AgentWeb with Apache License 2.0 6 votes vote down vote up
private static String getRealPathBelowVersion(Context context, Uri uri) {
	String filePath = null;
	LogUtils.i(TAG, "method -> getRealPathBelowVersion " + uri + "   path:" + uri.getPath() + "    getAuthority:" + uri.getAuthority());
	String[] projection = {MediaStore.Images.Media.DATA};
	CursorLoader loader = new CursorLoader(context, uri, projection, null,
			null, null);
	Cursor cursor = loader.loadInBackground();
	if (cursor != null) {
		cursor.moveToFirst();
		filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
		cursor.close();
	}
	if (filePath == null) {
		filePath = uri.getPath();
	}
	return filePath;
}
 
Example 2
Source File: MediaStoreRetriever.java    From Camera-Roll-Android-App with Apache License 2.0 6 votes vote down vote up
public static String getPathForUri(Context context, Uri uri) {
    CursorLoader cursorLoader = new CursorLoader(
            context, uri, new String[]{MediaStore.Files.FileColumns.DATA},
            null, null, null);

    try {
        final Cursor cursor = cursorLoader.loadInBackground();

        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            return cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
        }
        return null;
    } catch (SecurityException e) {
        Toast.makeText(context, "Permission Error", Toast.LENGTH_SHORT).show();
        return null;
    }
}
 
Example 3
Source File: FileUtil.java    From Rumble with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("NewApi")
private static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    CursorLoader cursorLoader = new CursorLoader(
            context,
            contentUri, 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;
}
 
Example 4
Source File: ImageChooserModule.java    From react-native-image-chooser with MIT License 6 votes vote down vote up
@Nullable
private String getPathFromUri(Uri contentUri) {
    if (contentUri.getScheme().equals("file")) {
        return contentUri.getPath();
    }

    String[] projection = {MediaStore.Images.Media.DATA};

    CursorLoader loader = new CursorLoader(getReactApplicationContext(), contentUri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();

    try {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        return cursor.getString(column_index);
    } catch (RuntimeException e) {
        return null;
    } finally {
        cursor.close();
    }
}
 
Example 5
Source File: Utils.java    From Android-ImageManager with MIT License 6 votes vote down vote up
/**
 * Gets the corresponding path to a file from the given content:// URI
 * <p>
 * This must run on the main thread
 * 
 * @param context
 * @param uri
 * @return the file path as a string
 */
public static String getContentPathFromUri(final Context context, final Uri uri) {
    Cursor cursor = null;
    String contentPath = null;
    try {
        final String[] proj = { MediaStore.Images.Media.DATA };
        final CursorLoader loader = new CursorLoader(context, uri, proj, null, null, null);
        cursor = loader.loadInBackground();
        final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        contentPath = cursor.getString(columnIndex);
    } catch (final Exception e) {
        Log.w(TAG, "getContentPathFromURI(" + uri.toString() + "): " + e.getMessage());
    } finally {
        if (cursor != null)
            cursor.close();
    }

    return contentPath != null ? contentPath : "";

}
 
Example 6
Source File: ChatFragment.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: AgentWebX5Utils.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
private static String getRealPathBelowVersion(Context context, Uri uri) {
    String filePath = null;
    String[] projection = {MediaStore.Images.Media.DATA};

    CursorLoader loader = new CursorLoader(context, uri, projection, null,
            null, null);
    Cursor cursor = loader.loadInBackground();

    if (cursor != null) {
        cursor.moveToFirst();
        filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
        cursor.close();
    }
    return filePath;
}
 
Example 8
Source File: GalleryHelper.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns a path, given a content URI.
 *
 * @param contentUri URI of some image
 * @return A string path
 */
public String getRealPathFromURI(Uri contentUri) {
  String[] proj = {MediaStore.Images.Media.DATA};
  CursorLoader cLoader = new CursorLoader(activity.getApplicationContext(), contentUri, proj, null, null, null);
  Cursor cursor = cLoader.loadInBackground();
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}
 
Example 9
Source File: FileUtils.java    From buddycloud-android with Apache License 2.0 5 votes vote down vote up
public static String getRealPathFromURI(Context context, Uri contentUri) {
    if (contentUri.getScheme().equals("file")) {
    	return contentUri.getSchemeSpecificPart();
    }
	String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(columnIndex);
}
 
Example 10
Source File: MediaStoreRetriever.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
@Override
void loadAlbums(final Activity context, boolean hiddenFolders) {

    final long startTime = System.currentTimeMillis();

    final ArrayList<Album> albums = new ArrayList<>();

    // Return only video and image metadata.
    String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
            + " OR "
            + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

    Uri queryUri = MediaStore.Files.getContentUri("external");

    CursorLoader cursorLoader = new CursorLoader(
            context,
            queryUri,
            projection,
            selection,
            null, // Selection args (none).
            MediaStore.Files.FileColumns.DATE_ADDED);

    final Cursor cursor = cursorLoader.loadInBackground();

    if (cursor == null) {
        return;
    }

    //search hiddenFolders
    if (hiddenFolders) {
        ArrayList<Album> hiddenAlbums = checkHiddenFolders(context);
        albums.addAll(hiddenAlbums);
    }

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            if (cursor.moveToFirst()) {
                String path;
                long dateTaken, id;
                int pathColumn = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
                int idColumn = cursor.getColumnIndex(BaseColumns._ID);

                do {
                    path = cursor.getString(pathColumn);
                    AlbumItem albumItem = AlbumItem.getInstance(context, path);
                    if (albumItem != null) {
                        //set dateTaken
                        int dateTakenColumn = cursor.getColumnIndex(
                                !(albumItem instanceof Video) ?
                                        MediaStore.Images.ImageColumns.DATE_TAKEN :
                                        MediaStore.Video.VideoColumns.DATE_TAKEN);
                        dateTaken = cursor.getLong(dateTakenColumn);
                        albumItem.setDate(dateTaken);

                        id = cursor.getLong(idColumn);
                        Uri uri = ContentUris.withAppendedId(
                                MediaStore.Files.getContentUri("external"), id);
                        albumItem.setUri(uri);

                        //search bucket
                        boolean foundBucket = false;
                        for (int i = 0; i < albums.size(); i++) {
                            if (albums.get(i).getPath().equals(FileOperation.Util.getParentPath(path))) {
                                albums.get(i).getAlbumItems().add(0, albumItem);
                                foundBucket = true;
                                break;
                            }
                        }

                        if (!foundBucket) {
                            //no bucket found
                            String bucketPath = FileOperation.Util.getParentPath(path);
                            if (bucketPath != null) {
                                albums.add(new Album().setPath(bucketPath));
                                albums.get(albums.size() - 1).getAlbumItems().add(0, albumItem);
                            }
                        }
                    }

                } while (cursor.moveToNext());

            }
            cursor.close();

            //done loading media with content resolver
            MediaProvider.OnMediaLoadedCallback callback = getCallback();
            if (callback != null) {
                callback.onMediaLoaded(albums);
            }

            Log.d("MediaStoreRetriever", "onMediaLoaded(): "
                    + String.valueOf(System.currentTimeMillis() - startTime) + " ms");
        }
    });
}