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:Main.java

public static int getContactIdFromPhoneNumber(final Context context, final String number) {
    final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    final String[] projection = { PhoneLookup._ID };
    final Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();/* w  w w.  ja v  a 2  s.co m*/
        return c.getInt(0);
    } else {
        return -1;
    }
}

From source file:Main.java

public static Uri getMediaUri(Cursor cursor, Uri uri) {
    String id = cursor.getString(cursor.getColumnIndex(MediaColumns._ID));
    return Uri.withAppendedPath(uri, id);
}

From source file:Main.java

public static void openContactInfo(final Context context, final long contactid) {
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    final Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid));
    intent.setData(uri);//from   w ww .  j av  a2 s.  c  o m
    context.startActivity(intent);
}

From source file:Main.java

private static InputStream openDisplayPhoto(final Context context, final Uri contactUri) {
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
    try {/*  w  w  w.  ja v  a2s .c om*/
        AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
        return fd.createInputStream();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

private static InputStream openPhoto(final Context context, final Uri contactUri) {
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null);
    if (cursor == null) {
        return null;
    }/*from  ww  w.  j a  va2  s .c  o  m*/
    try {
        if (cursor.moveToFirst()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return new ByteArrayInputStream(data);
            }
        }
    } finally {
        cursor.close();
    }
    return null;
}

From source file:Main.java

public static Cursor getAllCallLogAboutACaller(Context context, String callerNumber) {
    // cancello la chiamata in uscita se nelle preferenze ? settata tale opzione
    Uri delUri = Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, "");
    Cursor cursor = context.getContentResolver().query(delUri, null,
            android.provider.CallLog.Calls.NUMBER + "=?", new String[] { "404" }, null);
    try {//from  www.  j  ava  2 s  . c  om
        boolean moveToFirst = cursor.moveToFirst();
        Log.i("MOVETOFIRST", "moveToFirst=" + moveToFirst);
        do {
            int numberColumn = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
            String callerPhoneNumber = cursor.getString(numberColumn);
            Log.i(TAG, "numero : " + callerPhoneNumber);
        } while (cursor.moveToNext());

    } catch (Exception e) {
        Log.e(TAG, "Problem moving to first entry", e);
    }
    return cursor;
}

From source file:Main.java

/**
 * @param mContext//from   w ww. j a  v  a  2  s  .com
 * @param number
 * @return sunrise.l String 2012-5-28
 */
public static String getNameFormNumber(Context mContext, String number) {
    String name = null;

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
    Cursor cur = mContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cur != null && cur.moveToFirst()) {
        int nameIndex = cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        name = cur.getString(nameIndex);
    }
    cur.close();
    return name;
}

From source file:Main.java

public static String getPersonNameFromNumber(Context context, String box, String address) {
    if (address == null) {
        return "unknown";
    }/*w w w.  jav  a2s. c  o  m*/
    if (!box.equalsIgnoreCase("draft")) {
        Cursor cursor = context.getContentResolver().query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)),
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.getCount() > 0) {
                    cursor.moveToFirst();
                    String name = cursor.getString(0);
                    return name;
                }
            } finally {
                cursor.close();
            }
        }
    }
    if (address != null) {
        return PhoneNumberUtils.formatNumber(address);
    }
    return "unknown";
}

From source file:Main.java

/**
 * Looks up contact name in the address book by the phone number.
 * //w  w w .j  a v  a  2 s. c  o m
 * @param context the Android context.
 * @param phone the phone number to look up.
 * @return a contact name.
 */
public static String lookupNameByPhone(Context context, String phone) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID };
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    try {
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (name != null && name.length() > 0) {
                return name;
            }
        }
    } finally {
        cursor.close();
    }

    return phone;
}

From source file:Main.java

public static Bundle getContactFromNumber(String number, Context context) {
    ContentResolver cr = context.getContentResolver();
    String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
    Bundle bundle = new Bundle();
    Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor = cr.query(contactUri, projection, null, null, null);
    try {// ww w .j a  va  2 s. c o m
        if (cursor.moveToFirst()) {
            for (String key : projection) {
                bundle.putString(key, cursor.getString(cursor.getColumnIndex(key)));
            }
        }
        return bundle;
    } catch (Exception e) {
        return null;
    } finally {
        cursor.close();
    }

}