Example usage for android.support.v4.content CursorLoader loadInBackground

List of usage examples for android.support.v4.content CursorLoader loadInBackground

Introduction

In this page you can find the example usage for android.support.v4.content CursorLoader loadInBackground.

Prototype

@Override
    public Cursor loadInBackground() 

Source Link

Usage

From source file:Main.java

public static File parseFileByIntentData(Context context, Intent data) {
    File file = null;/*  ww w .  j  av a 2  s  .c om*/
    if (data != null && data.getData() != null) {
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, data.getData(), proj, null, null, null);
        Cursor cursor = null;
        try {
            cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            file = new File(cursor.getString(column_index));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    return file;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 */// w w w  .j a va2s .c  o m
public static String getRealPathFromURI(Activity activity, Uri contentUri) {

    String releaseNumber = Build.VERSION.RELEASE;

    if (releaseNumber != null) {
        /* ICS Version */
        if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
            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();
            }
            return strFileName;
        }
        /* GB Version */
        else if (releaseNumber.startsWith("2.3")) {
            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();
            }
            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:org.disrupted.rumble.util.FileUtil.java

@SuppressLint("NewApi")
private static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;//  w  ww  .  j  a v a 2  s .  c  om

    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;
}

From source file:com.marvin.rocklock.PlaylistUtils.java

/**
 * Gets the playlist ID given it's name//from  w  w w .jav  a2s .  c om
 *
 * @param context The activity calling this method
 * @param name The playlist name
 * @return The playlist ID
 */
public static int getPlaylistId(RockLockActivity context, String name) {
    String[] proj = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME };

    String formattedName = name.replace("'", "''");
    String filter = MediaStore.Audio.Playlists.NAME + "= '" + formattedName + "'";

    CursorLoader loader = new CursorLoader(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, proj,
            filter, null, null);
    Cursor testCursor = loader.loadInBackground();

    int idx = -1;
    boolean hasFirst = testCursor.moveToFirst();
    if (hasFirst) {
        idx = testCursor.getInt(0);
    }
    testCursor.close();
    return idx;
}

From source file:com.marvin.rocklock.PlaylistUtils.java

/**
 * Adds a given song to an existing playlist
 *
 * @param context the managing activity//from ww  w.ja  v  a2  s.  c  om
 * @param id the song id to add
 */
public static void showPlaylistDialog(final RockLockActivity context, final String id) {

    // Get list of playlists
    String[] proj = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME };
    CursorLoader loader = new CursorLoader(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, proj, null,
            null, null);
    final Cursor playlistCursor = loader.loadInBackground();
    // Show playlists
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    if (playlistCursor.moveToFirst()) {
        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                addToPlaylist(context, playlistCursor.getInt(0), id);
            }

        };
        builder.setCursor(playlistCursor, clickListener, MediaStore.Audio.Playlists.NAME);
    } else {
        // No playlists: show create dialog
        builder.setTitle(context.getString(R.string.new_playlist));
        // TODO(sainsley): add default name based on
        // number of playlists in directory
        builder.setMessage(context.getString(R.string.enter_playlist_name));

        final EditText input = new EditText(context);
        builder.setView(input);

        builder.setPositiveButton(context.getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                String name = input.getText().toString();
                ArrayList<String> ids = new ArrayList<String>();
                ids.add(id);
                writePlaylist(context, name, ids);
                return;
            }
        });
    }
    builder.show();
}

From source file:util.mediamanager.PlaylistUtils.java

/**
 * Adds a given song to an existing playlist
 *
 * @param context the managing activity// w  w w .ja  v a2s. c  om
 * @param id the song id to add
 */
public static void showPlaylistDialog(final Context context, final String id) {

    // Get list of playlists
    String[] proj = { MediaStore.Audio.Playlists._ID, "playlist_name" };
    CursorLoader loader = new CursorLoader(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, proj, null,
            null, null);
    final Cursor playlistCursor = loader.loadInBackground();
    // Show playlists
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    if (playlistCursor.moveToFirst()) {
        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //addToPlaylist(context, playlistCursor.getInt(0), id);
            }
        };
        builder.setCursor(playlistCursor, clickListener, "playlist_name");
    } else {
        // No playlists: show create dialog
        builder.setTitle("Playlist");
        // TODO(sainsley): add default name based on
        // number of playlists in directory
        builder.setMessage("Enter Playlist Name");

        final EditText input = new EditText(context);
        builder.setView(input);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                String name = input.getText().toString();
                ArrayList<String> ids = new ArrayList<String>();
                ids.add(id);
                //writePlaylist(context, name, ids);
                return;
            }
        });
    }
    builder.show();
}

From source file:util.mediamanager.PlaylistUtils.java

/**
 * Gets the playlist ID given it's name/*from  w  w w  .j a  v a2  s  .c om*/
 *
 * @param context The activity calling this method
 * @param name The playlist name
 * @return The playlist ID
 */
public static int getPlaylistId(Context context, String name) {
    String[] projExternalFilePlaylist = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME };

    // filter
    String formattedName = name.replace("'", "''");
    String filter = MediaStore.Audio.Playlists.NAME + "= '" + formattedName + "'";

    CursorLoader loader = new CursorLoader(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
            projExternalFilePlaylist, /*filter*/ null, null, null);

    Cursor testCursor = loader.loadInBackground();

    int idx = -1;
    boolean hasFirst = testCursor.moveToFirst();
    String sName = "";
    if (hasFirst) {
        idx = testCursor.getInt(0);
        sName = testCursor.getString(1);
        Log.d(Constants.APP.TAG, "idx=" + idx + ", sName = " + sName);
    }
    while (testCursor.moveToNext()) {
        idx = testCursor.getInt(0);
        sName = testCursor.getString(1);
        Log.d(Constants.APP.TAG, "idx=" + idx + ", sName = " + sName);
    }
    testCursor.close();

    String[] projGMP = { MediaStore.Audio.Media._ID, MediaStore.Audio.AudioColumns.TITLE,
            MediaStore.Audio.Media.ARTIST };
    Uri gMusicUri = Uri.parse("content://com.google.android.music.MusicContent/playlists");
    Cursor cur2 = context.getContentResolver().query(gMusicUri, new String[] { "_id", "playlist_name" }, null,
            null, null);

    hasFirst = cur2.moveToFirst();
    if (hasFirst) {
        idx = cur2.getInt(0);
        sName = cur2.getString(1);
        Log.d(Constants.APP.TAG, "idx=" + idx + ", sName = " + sName);
    }
    while (cur2.moveToNext()) {
        idx = cur2.getInt(0);
        sName = cur2.getString(1);
        Log.d(Constants.APP.TAG, "idx=" + idx + ", sName = " + sName);
    }
    cur2.close();
    return idx;
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * ?/*w  ww .  ja va 2s .  co  m*/
 *
 * @param data 
 * @return ?
 */
public static String readBitmapFromAlbumResult(Context context, Intent data) {
    Uri imageUri = data.getData();
    if (imageUri == null) {
        return null;
    } else if (imageUri.toString().startsWith("file:///")) {
        try {
            // ??
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(imageUri.getPath(), options);
            return imageUri.getPath();
        } catch (Exception ex) {
            Toast.makeText(context.getApplicationContext(),
                    "?", Toast.LENGTH_SHORT).show();
            return null;
        }
    } else {
        String[] projection = { MediaStore.MediaColumns.DATA };
        CursorLoader cursorLoader = new CursorLoader(context, imageUri, projection, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String selectedImagePath = cursor.getString(column_index);
        cursor.close();
        return selectedImagePath;
    }
}

From source file:com.just.agentweb.AgentWebUtils.java

private static String getRealPathBelowVersion(Context context, Uri uri) {
    String filePath = null;/*from   ww  w .  j a v  a  2  s. c o  m*/
    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;
}

From source file:com.microsoft.band.sdksample.ThemeFragment.java

private String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getActivity(), uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();/* www. j a va2  s  . c o  m*/
    return cursor.getString(column_index);
}