Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Uri value to bundle value

This following code fetches multiple contacts and stores them in a bundle:

 Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
    phonebookIntent.putExtra("additional", "phone-multi");
    phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
    phonebookIntent.putExtra("FromMMS", true);
    startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);

The code below retrieves the selected data:

  if (requestCode ==  REQUEST_CODE_PICK_CONTACT) {

            Uri contractData = data.getData();

            Bundle contantData =  data.getExtras();
            String result= contantData.getString("result");
            ArrayList<String> contacts = contantData.getStringArrayList("result");
            ContactRetriever cr = new ContactRetriever(getApplicationContext(), contacts);
            Person p = cr.getPerson();

I want the data in Uri so that I can make use of it in other classes which refer to Uri data.

How can I pass the bundle value to the Uri data?

Another example of using Uri data as "contactData":

private String getNumber() {
    String ret = null;


    Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null);

    if (cId.moveToFirst()){
        id = cId.getString(cId.getColumnIndex(ContactsContract.Contacts._ID));
        Log.i(TAG + " IDs: ", id);
    }
    cId.close();

    Cursor cNum = cr.query(contractData, null, null, null, null);

    if (cNum.moveToNext()){
        ret = cNum.getString(cNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Log.i(ret, TAG + " NUMBERS: ");
    }
    return ret;
}

private String getName() {
    String ret = null;

    Cursor c = cr.query(contractData, null, null, null, null);
    if (c.moveToFirst())
        ret = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    c.close();
    Log.i(TAG + "NAMES: ", ret);
    return ret;
}

Specific usage of contractData examples:

Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null);

Cursor cNum = cr.query(contractData, null, null, null, null);

Cursor c = cr.query(contractData, null, null, null, null);
like image 815
Meghana Avatar asked Jan 17 '17 08:01

Meghana


People also ask

How do I pass URI?

How do I pass a URI? private Uri imageUri; .... Intent intent = new Intent(this, GoogleActivity. class); intent.

How can I get data from URI?

To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.


2 Answers

Uri implements Parcelable so you can do it this way:

phonebookIntent.putExtra("uri", your_uri_object);

Read it back:

Uri uri =  getIntent().getParcelableExtra("uri");
like image 179
Avinash Roy Avatar answered Nov 01 '22 15:11

Avinash Roy


You can pass the Uri as a string using the toString function, then parse it back as a Uri.

So, to send it as part of the URI:

phonebookIntent.putExtra("uri", uriData.toString());

To read it back:

Uri uriData = Uri.parse(extras.getString"uri"));
like image 41
greysqrl Avatar answered Nov 01 '22 15:11

greysqrl