Example usage for android.content ContentUris withAppendedId

List of usage examples for android.content ContentUris withAppendedId

Introduction

In this page you can find the example usage for android.content ContentUris withAppendedId.

Prototype

public static Uri withAppendedId(Uri contentUri, long id) 

Source Link

Document

Appends the given ID to the end of the path.

Usage

From source file:com.cyanogenmod.eleven.utils.PlaylistPopupMenuHelper.java

/**
 * Create a new {@link AlertDialog} for easy playlist deletion
 * /*from   w w  w .j a v a 2  s.  co m*/
 * @param playlistName The title of the playlist being deleted
 * @param playlistId The ID of the playlist being deleted
 * @return A new {@link AlertDialog} used to delete playlists
 */
private final AlertDialog buildDeleteDialog(final long playlistId, final String playlistName) {
    return new AlertDialog.Builder(mActivity)
            .setTitle(mActivity.getString(R.string.delete_dialog_title, playlistName))
            .setPositiveButton(R.string.context_menu_delete, new OnClickListener() {

                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    final Uri mUri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                            playlistId);
                    mActivity.getContentResolver().delete(mUri, null, null);
                    MusicUtils.refresh();
                }
            }).setNegativeButton(R.string.cancel, new OnClickListener() {

                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    dialog.dismiss();
                }
            }).setMessage(R.string.cannot_be_undone).create();
}

From source file:com.maskyn.fileeditorpro.util.AccessStorageApi.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.//from   ww  w . ja  va 2 s .  c  o m
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 */
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    String path = "";

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    try {
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            if (isTurboDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                path = "/" + split[1];
            }
            // ExternalStorageProvider
            else if (isExternalStorageDocument(uri)) {

                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    path = Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris
                        .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                path = getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                path = getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            path = getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            path = uri.getPath();
        }
    } catch (Exception ex) {
        return "";
    }

    return path;
}

From source file:com.manning.androidhacks.hack043.provider.MySQLContentProvider.java

@Override
protected Uri insertInTransaction(Uri uri, ContentValues values) {
    String table = null;//from  w w  w.  j  av  a  2  s  .  c  o  m

    switch (sUriMatcher.match(uri)) {
    case ITEM:
        table = TABLE_NAME;
        break;
    default:
        new RuntimeException("Invalid URI for inserting: " + uri);
    }

    long rowId = mDb.insert(table, null, values);

    if (rowId > 0) {
        Uri noteUri = ContentUris.withAppendedId(uri, rowId);
        return noteUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
}

From source file:at.bitfire.ical4android.AndroidCalendar.java

public static AndroidCalendar findByID(Account account, ContentProviderClient provider,
        AndroidCalendarFactory factory, long id) throws FileNotFoundException, CalendarStorageException {
    @Cleanup// w w w .java  2s .com
    EntityIterator iterCalendars = null;
    try {
        iterCalendars = CalendarContract.CalendarEntity.newEntityIterator(provider.query(
                syncAdapterURI(ContentUris.withAppendedId(CalendarContract.CalendarEntity.CONTENT_URI, id),
                        account),
                null, null, null, null));
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query calendars", e);
    }

    if (iterCalendars.hasNext()) {
        ContentValues values = iterCalendars.next().getEntityValues();

        AndroidCalendar calendar = factory.newInstance(account, provider, values.getAsLong(Calendars._ID));
        calendar.populate(values);
        return calendar;
    }
    throw new FileNotFoundException();
}

From source file:edu.mit.mobile.android.locast.data.JsonSyncableItem.java

/**
 * Given a public Uri fragment, finds the local item representing it. If there isn't any such item, null is returned.
 *
 * @param context//from w w w  .j  av a2 s  .  co  m
 * @param dirUri the base local URI to search.
 * @param pubUri A public URI fragment that represents the given item. This must match the result from the API.
 * @return a local URI matching the item or null if none were found.
 */
public static Uri getItemByPubIUri(Context context, Uri dirUri, String pubUri) {
    Uri uri = null;
    final ContentResolver cr = context.getContentResolver();

    final String[] selectionArgs = { pubUri };
    final Cursor c = cr.query(dirUri, PUB_URI_PROJECTION, _PUBLIC_URI + "=?", selectionArgs, null);
    if (c.moveToFirst()) {
        uri = ContentUris.withAppendedId(dirUri, c.getLong(c.getColumnIndex(_ID)));
    }

    c.close();
    return uri;
}

From source file:edu.mit.mobile.android.locast.data.JsonSyncableItem.java

public Uri getCanonicalUri() {
    return ContentUris.withAppendedId(getContentUri(), getLong(getColumnIndex(_ID)));
}

From source file:com.ksk.droidbatterybooster.provider.TimeSchedule.java

public static Uri getUri(int scheduleId) {
    return ContentUris.withAppendedId(CONTENT_URI, scheduleId);
}

From source file:com.android.erowser.DownloadTouchIcon.java

@Override
public void onPostExecute(Bitmap icon) {
    // Do this first in case the download failed.
    if (mActivity != null) {
        // Remove the touch icon loader from the ErowserActivity.
        mActivity.mTouchIconLoader = null;
    }//w ww.  j a  va2  s .c o m

    if (icon == null || mCursor == null || isCancelled()) {
        return;
    }

    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.PNG, 100, os);
    ContentValues values = new ContentValues();
    values.put(ErowserP.BookmarkColumns.TOUCH_ICON, os.toByteArray());

    if (mCursor.moveToFirst()) {
        do {
            mContentResolver.update(ContentUris.withAppendedId(ErowserP.BOOKMARKS_URI, mCursor.getInt(0)),
                    values, null, null);
        } while (mCursor.moveToNext());
    }
    mCursor.close();
}

From source file:com.android.browser.kai.DownloadTouchIcon.java

@Override
public void onPostExecute(Bitmap icon) {
    // Do this first in case the download failed.
    if (mActivity != null) {
        // Remove the touch icon loader from the BrowserActivity.
        mActivity.mTouchIconLoader = null;
    }//from w ww .  j  a  va2 s. c om

    if (icon == null || mCursor == null || isCancelled()) {
        return;
    }

    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.PNG, 100, os);
    ContentValues values = new ContentValues();
    values.put(Browser.BookmarkColumns.TOUCH_ICON, os.toByteArray());

    if (mCursor.moveToFirst()) {
        do {
            mContentResolver.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI, mCursor.getInt(0)),
                    values, null, null);
        } while (mCursor.moveToNext());
    }
    mCursor.close();
}

From source file:co.nerdart.ourss.fragment.EntriesListFragment.java

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
    startActivity(new Intent(Intent.ACTION_VIEW, ContentUris.withAppendedId(mUri, id)));
}