Example usage for android.net Uri withAppendedPath

List of usage examples for android.net Uri withAppendedPath

Introduction

In this page you can find the example usage for android.net Uri withAppendedPath.

Prototype

public static Uri withAppendedPath(Uri baseUri, String pathSegment) 

Source Link

Document

Creates a new Uri by appending an already-encoded path segment to a base Uri.

Usage

From source file:com.nextgis.maplibui.activity.ModifyAttributesActivity.java

protected void putSign() {
    LinearLayout layout = (LinearLayout) findViewById(R.id.controls_list);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        if (child instanceof Sign) {
            IGISApplication application = (IGISApplication) getApplication();
            Uri uri = Uri.parse("content://" + application.getAuthority() + "/" + mLayer.getPath().getName()
                    + "/" + mFeatureId + "/" + Constants.URI_ATTACH);

            ContentValues values = new ContentValues();
            values.put(VectorLayer.ATTACH_DISPLAY_NAME, "_signature");
            values.put(VectorLayer.ATTACH_DESCRIPTION, "_signature");
            values.put(VectorLayer.ATTACH_MIME_TYPE, "image/jpeg");

            Cursor saved = getContentResolver().query(uri, null, VectorLayer.ATTACH_ID + " =  ?",
                    new String[] { Sign.SIGN_FILE }, null);
            boolean hasSign = false;
            if (saved != null) {
                hasSign = saved.moveToFirst();
                saved.close();//from w  w  w  . ja  v  a 2  s. com
            }

            if (!hasSign) {
                Uri result = getContentResolver().insert(uri, values);
                if (result != null) {
                    long id = Long.parseLong(result.getLastPathSegment());
                    values.clear();
                    values.put(VectorLayer.ATTACH_ID, Integer.MAX_VALUE);
                    uri = Uri.withAppendedPath(uri, id + "");
                    getContentResolver().update(uri, values, null, null);
                }
            }

            File png = new File(mLayer.getPath(), mFeatureId + "");
            Sign sign = (Sign) child;
            try {
                if (!png.isDirectory())
                    FileUtil.createDir(png);

                png = new File(png, Sign.SIGN_FILE);
                sign.save(sign.getWidth(), sign.getHeight(), true, png);
            } catch (IOException | RuntimeException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.android.contacts.common.model.ContactLoader.java

/**
 * Extracts Contact level columns from the cursor.
 *//*from www.  j  ava2  s.c o m*/
private Contact loadContactHeaderData(final Cursor cursor, Uri contactUri) {
    final String directoryParameter = contactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
    final long directoryId = directoryParameter == null ? Directory.DEFAULT
            : Long.parseLong(directoryParameter);
    final long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
    final String lookupKey = cursor.getString(ContactQuery.LOOKUP_KEY);
    final long nameRawContactId = cursor.getLong(ContactQuery.NAME_RAW_CONTACT_ID);
    final int displayNameSource = cursor.getInt(ContactQuery.DISPLAY_NAME_SOURCE);
    final String displayName = cursor.getString(ContactQuery.DISPLAY_NAME);
    final String altDisplayName = cursor.getString(ContactQuery.ALT_DISPLAY_NAME);
    final String phoneticName = cursor.getString(ContactQuery.PHONETIC_NAME);
    final long photoId = cursor.getLong(ContactQuery.PHOTO_ID);
    final String photoUri = cursor.getString(ContactQuery.PHOTO_URI);
    final boolean starred = cursor.getInt(ContactQuery.STARRED) != 0;
    final Integer presence = cursor.isNull(ContactQuery.CONTACT_PRESENCE) ? null
            : cursor.getInt(ContactQuery.CONTACT_PRESENCE);
    final boolean sendToVoicemail = cursor.getInt(ContactQuery.SEND_TO_VOICEMAIL) == 1;
    final String customRingtone = cursor.getString(ContactQuery.CUSTOM_RINGTONE);
    final boolean isUserProfile = cursor.getInt(ContactQuery.IS_USER_PROFILE) == 1;

    Uri lookupUri;
    if (directoryId == Directory.DEFAULT || directoryId == Directory.LOCAL_INVISIBLE) {
        lookupUri = ContentUris.withAppendedId(Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey),
                contactId);
    } else {
        lookupUri = contactUri;
    }

    return new Contact(mRequestedUri, contactUri, lookupUri, directoryId, lookupKey, contactId,
            nameRawContactId, displayNameSource, photoId, photoUri, displayName, altDisplayName, phoneticName,
            starred, presence, sendToVoicemail, customRingtone, isUserProfile);
}

From source file:im.vector.activity.CallViewActivity.java

/**
 * Provices a ringtone from a resource and a filename.
 * The audio file must have a ANDROID_LOOP metatada set to true to loop the sound.
 * @param resid The audio resource.//from   ww  w .  jav a 2  s.  co m
 * @param filename the audio filename
 * @return a RingTone, null if the operation fails.
 */
static Ringtone getRingTone(Context context, int resid, String filename) {
    Ringtone ringtone = null;

    try {
        Uri ringToneUri = null;
        File directory = new File(Environment.getExternalStorageDirectory(),
                "/" + context.getApplicationContext().getPackageName().hashCode() + "/Audio/");

        // create the directory if it does not exist
        if (!directory.exists()) {
            directory.mkdirs();
        }

        File file = new File(directory + "/", filename);

        // if the file exists, check if the resource has been created
        if (file.exists()) {
            Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.DATA + "=? ",
                    new String[] { file.getAbsolutePath() }, null);

            if ((null != cursor) && cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                ringToneUri = Uri.withAppendedPath(Uri.parse("content://media/external/audio/media"), "" + id);
            }

            if (null != cursor) {
                cursor.close();
            }
        }

        // the Uri has been received
        if (null == ringToneUri) {
            // create the file
            if (!file.exists()) {
                try {
                    byte[] readData = new byte[1024];
                    InputStream fis = context.getResources().openRawResource(resid);
                    FileOutputStream fos = new FileOutputStream(file);
                    int i = fis.read(readData);

                    while (i != -1) {
                        fos.write(readData, 0, i);
                        i = fis.read(readData);
                    }

                    fos.close();
                } catch (Exception e) {
                }
            }

            // and the resource Uri
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, filename);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
            values.put(MediaStore.MediaColumns.SIZE, file.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);

            ringToneUri = context.getContentResolver()
                    .insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), values);
        }

        if (null != ringToneUri) {
            ringtone = RingtoneManager.getRingtone(context, ringToneUri);
        }
    } catch (Exception e) {

    }

    return ringtone;
}

From source file:org.matrix.console.activity.CallViewActivity.java

/**
 * Provices a ringtone from a resource and a filename.
 * The audio file must have a ANDROID_LOOP metatada set to true to loop the sound.
 * @param resid The audio resource.//from  w  w  w.  java2  s . c  o m
 * @param filename the audio filename
 * @return a RingTone, null if the operation fails.
 */
static Ringtone getRingTone(Context context, int resid, String filename) {
    Ringtone ringtone = null;

    try {
        Uri ringToneUri = null;
        File directory = new File(Environment.getExternalStorageDirectory(),
                "/" + context.getApplicationContext().getPackageName().hashCode() + "/Audio/");

        // create the directory if it does not exist
        if (!directory.exists()) {
            directory.mkdirs();
        }

        File file = new File(directory + "/", filename);

        // if the file exists, check if the resource has been created
        if (file.exists()) {
            Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.DATA + "=? ",
                    new String[] { file.getAbsolutePath() }, null);

            if ((null != cursor) && cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                ringToneUri = Uri.withAppendedPath(Uri.parse("content://media/external/audio/media"), "" + id);
            }
        }

        // the Uri has been received
        if (null == ringToneUri) {
            // create the file
            if (!file.exists()) {
                try {
                    byte[] readData = new byte[1024];
                    InputStream fis = context.getResources().openRawResource(resid);
                    FileOutputStream fos = new FileOutputStream(file);
                    int i = fis.read(readData);

                    while (i != -1) {
                        fos.write(readData, 0, i);
                        i = fis.read(readData);
                    }

                    fos.close();
                } catch (Exception e) {
                }
            }

            // and the resource Uri
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, filename);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
            values.put(MediaStore.MediaColumns.SIZE, file.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);

            ringToneUri = context.getContentResolver()
                    .insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), values);
        }

        if (null != ringToneUri) {
            ringtone = RingtoneManager.getRingtone(context, ringToneUri);
        }
    } catch (Exception e) {

    }

    return ringtone;
}

From source file:com.example.contactslist.ui.ContactsListFragment.java

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // If this is the loader for finding contacts in the Contacts Provider
    // (the only one supported)
    if (id == ContactsQuery.QUERY_ID) {
        Uri contentUri;/*from w  w w .  j a va  2  s.  c  o  m*/

        // There are two types of searches, one which displays all contacts and
        // one which filters contacts by a search query. If mSearchTerm is set
        // then a search query has been entered and the latter should be used.

        if (mSearchTerm == null) {
            // Since there's no search string, use the content URI that searches the entire
            // Contacts table
            contentUri = ContactsQuery.CONTENT_URI;
        } else {
            // Since there's a search string, use the special content Uri that searches the
            // Contacts table. The URI consists of a base Uri and the search string.
            contentUri = Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm));
        }

        // Returns a new CursorLoader for querying the Contacts table. No arguments are used
        // for the selection clause. The search string is either encoded onto the content URI,
        // or no contacts search string is used. The other search criteria are constants. See
        // the ContactsQuery interface.

        return new CursorLoader(getActivity(), contentUri, ContactsQuery.PROJECTION, null, null,
                ContactsQuery.SORT_ORDER);
    }

    Log.e(TAG, "onCreateLoader - incorrect ID provided (" + id + ")");
    return null;
}

From source file:at.flack.MainActivity.java

private static Integer fetchThumbnailId(Activity context, String number) {
    if (number == null || number.length() == 0)
        return null;
    final Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
            Uri.encode(number));//from  w w w.  j a v a 2 s .c  o  m
    final Cursor cursor = context.getContentResolver().query(uri, SMSItem.PHOTO_ID_PROJECTION, null, null,
            ContactsContract.Contacts.DISPLAY_NAME + " ASC");

    try {
        Integer thumbnailId = null;
        if (cursor.moveToFirst()) {
            thumbnailId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
        }
        return thumbnailId;
    } finally {
        cursor.close();
    }

}

From source file:nl.sogeti.android.gpstracker.viewer.LoggerMap.java

/**
 * Alter this to set a new track as current.
 *
 * @param trackId/*from   www  .j  a v a2s  .  c  o m*/
 * @param center  center on the end of the track
 */
private void moveToTrack(long trackId, boolean center) {
    Cursor track = null;
    try {
        ContentResolver resolver = this.getContentResolver();
        Uri trackUri = ContentUris.withAppendedId(Tracks.CONTENT_URI, trackId);
        track = resolver.query(trackUri, new String[] { Tracks.NAME }, null, null, null);
        if (track != null && track.moveToFirst()) {
            this.mTrackId = trackId;
            mLastSegment = -1;
            resolver.unregisterContentObserver(this.mTrackSegmentsObserver);
            resolver.unregisterContentObserver(this.mTrackMediasObserver);
            Uri tracksegmentsUri = Uri.withAppendedPath(Tracks.CONTENT_URI, trackId + "/segments");

            resolver.registerContentObserver(tracksegmentsUri, false, this.mTrackSegmentsObserver);
            resolver.registerContentObserver(Media.CONTENT_URI, true, this.mTrackMediasObserver);

            resetOverlay();

            updateTitleBar();
            updateDataOverlays();
            updateSpeedColoring();
            if (center) {
                GeoPoint lastPoint = getLastTrackPoint();
                this.mMapView.getController().animateTo(lastPoint);
            }
        }
    } finally {
        if (track != null) {
            track.close();
        }
    }
}

From source file:cx.ring.service.LocalService.java

private static void lookupDetails(@NonNull ContentResolver res, @NonNull CallContact c) {
    //Log.w(TAG, "lookupDetails " + c.getKey());
    try {/*from  www  .  ja  va 2s. c o m*/
        Cursor cPhones = res.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                CONTACTS_PHONES_PROJECTION, ID_SELECTION, new String[] { String.valueOf(c.getId()) }, null);
        if (cPhones != null) {
            final int iNum = cPhones.getColumnIndex(Phone.NUMBER);
            final int iType = cPhones.getColumnIndex(Phone.TYPE);
            final int iLabel = cPhones.getColumnIndex(Phone.LABEL);
            while (cPhones.moveToNext()) {
                c.addNumber(cPhones.getString(iNum), cPhones.getInt(iType), cPhones.getString(iLabel),
                        CallContact.NumberType.TEL);
                Log.w(TAG, "Phone:" + cPhones.getString(cPhones.getColumnIndex(Phone.NUMBER)));
            }
            cPhones.close();
        }

        Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, c.getId());
        Uri targetUri = Uri.withAppendedPath(baseUri, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
        Cursor cSip = res.query(targetUri, CONTACTS_SIP_PROJECTION,
                ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + " =?",
                new String[] { SipAddress.CONTENT_ITEM_TYPE, Im.CONTENT_ITEM_TYPE }, null);
        if (cSip != null) {
            final int iMime = cSip.getColumnIndex(ContactsContract.Data.MIMETYPE);
            final int iSip = cSip.getColumnIndex(SipAddress.SIP_ADDRESS);
            final int iType = cSip.getColumnIndex(SipAddress.TYPE);
            final int iLabel = cSip.getColumnIndex(SipAddress.LABEL);
            while (cSip.moveToNext()) {
                String mime = cSip.getString(iMime);
                String number = cSip.getString(iSip);
                if (!mime.contentEquals(Im.CONTENT_ITEM_TYPE) || new SipUri(number).isRingId()
                        || "ring".equalsIgnoreCase(cSip.getString(iLabel)))
                    c.addNumber(number, cSip.getInt(iType), cSip.getString(iLabel), CallContact.NumberType.SIP);
                Log.w(TAG, "SIP phone:" + number + " " + mime + " ");
            }
            cSip.close();
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }
}

From source file:com.example.google.play.apkx.SampleDownloaderActivity.java

/**
 * This starts the movie by starting the video player activity with a
 * content URI for our custom APK extension content provider.
 *//*  w  w w  .  j  a  v  a  2 s. c  om*/
private void startMovie() {
    // launch the movie player activity
    Uri mediaUri = Uri.withAppendedPath(SampleZipFileProvider.ASSET_URI, "big_buck_bunny_720p_surround.m4v");
    Intent intent = new Intent();
    intent.setData(mediaUri);
    intent.putExtra(Intent.EXTRA_TITLE, mediaUri.getLastPathSegment());
    intent.setClass(this, SampleVideoPlayerActivity.class);
    startActivity(intent);
    finish();
}