I'm using a contact picker as such:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
Phone.CONTENT_URI
), CONTACT_PICKER_RESULT //1001
);
This type of contact picker picks out a specific phone number that a contact has.
I then get an ID for this contact with:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CONTACT_PICKER_RESULT || resultCode != RESULT_OK)
return;
String id = data.getData.getLastPathSegment();
}
However, when I use a PhoneLookup query, such as:
Cursor cur = getContentResolver().query(
Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(...) //phone number of contact is filled in
),
new String[] {
PhoneLookup.DISPLAY_NAME,
Phone._ID
}, null, null, null
);
if (!cur.moveToFirst())
return;
String id = cur.getString(
cur.getColumnIndex(Phone._ID)
);
The ID I get from the PhoneLookup is different from the onActivityResult. For example, the contact picker returns 1408 while the cursor returns 444.
How can I get:
data.getData().getLastPathSegment() equal to cur.getColumnIndex(...)?
You can't use a PhoneLookup in this case. You need to query Phone.CONTENT_URI to obtain the ID that matches up with the contact picker. The following code below does exactly this.
Cursor cur = getContentResolver().query(
Phone.CONTENT_URI,
new String[] {
Phone.DISPLAY_NAME,
Phone._ID
},
Phone.NORMALIZED_NUMBER + "=?",
new String[] {
... //phone number placed here
}, null);
if (!cur.moveToFirst())
return;
String id = cur.getString(cur.getColumnIndex(Phone._ID)));
Related
My purpose is to set a list that comes from API which includes phonenumbers of the users of my app. So i only need to see contact that are in this list. But I dont know how set below code to do this. I mean in the below it opens contacts with all of the contact that are in the phone. I dont need to see the all the contacts but the ones that are in the given list
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1 :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(contactData, null, null, null, null);
if (cur.getCount() > 0) {// thats mean some resutl has been found
if(cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("Names", name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
}
phones.close();
}
}
}
cur.close();
}
break;
}
}
Consider you already have a list which has data from API, and create new list which will have common elements from API list and ContentResolver, now to add elements to your desired list, in ContentResolver check if the PhoneNumber is in API list before adding it to output.
ArrayList<String> apiPhoneNumbers;// this data is populated by API
ArrayList<String> commonPhoneNumbers = new ArrayList<>();
//inside onActivityResult
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
if(apiPhoneNumbers.contains(phoneNumber)){//add to output only if it is in API list
commonPhoneNumbers.add(phoneNumber);
}
}
phones.close();
}
I have three Activities
Hobbies
Language
Add Photo
What my goal: I get value of the hobby and language activity and also photo one and send it to the other activity and with that knowledge i create PDF file
Problem since i have 3 activities if user first go to Hobby activity and fill fields and then go to the language activity and fill all fields and at the end user go to the photo activity and fetch the photo from phone media and then create PDF file by doing this way I get the data as well as picture in PDF .Here my code works perfect USER SELECT PHOTO AT THE END AFTER FILLING DATA IN OTHER ACTIVITIES but Problem occur when and if he selects image first and then fills data and create PDF what happens I only get data in my PDF no sign of picture !!
I wanna put all the code but that too much code
Sending picture
private void getting_data_from_fields_and_sending_on_click_listner() {
Intent i = new Intent (Add_Picture_Activity.this , Selecting_Activity.class) ;
ByteArrayOutputStream b = new ByteArrayOutputStream() ;
resultBmp.compress(Bitmap.CompressFormat.JPEG , 100 , b);
byte[] bytes = b.toByteArray();
i.putExtra("UserImage" , bytes);
startActivity(i);
}
receiving Picture
public void gettingInfoFromTheAddPictureActivity() {
bitmap = (Bitmap) getIntent().getParcelableExtra("UserImage");
byte [] bytes = getIntent().getByteArrayExtra("UserImage");
bitmap = BitmapFactory.decodeByteArray(bytes , 0 , bytes.length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100 , stream);
userImage = null ;
try {
userImage = Image.getInstance(stream.toByteArray());
}catch (Exception c){
}
}
try {
gettingInfoFromTheAddPictureActivity();
userImage.scaleToFit(80 , 80);
userImage.setAbsolutePosition(0,0) ;
p.getDirectContent();
document.add(userImage);
}catch (Exception x){
}
Some How i solved the problem
First i get the path of the user selected image and store it in pref and then use that path to create a bitmap in other activity and then with the bitmap i load image to the PDF File ..
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri imageUri = data.getData( );
picturePath = getPath( Add_Picture_Activity.this, imageUri );
Log.d("Picture Path", picturePath);
}else{
Toast.makeText(this, "You have not select image", Toast.LENGTH_SHORT).show();
}
}
public static String getPath(Context context, Uri uri ) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
return result;
}
private void getting_data_from_fields_and_sending_on_click_listner() {
Intent sendingPicPath = new Intent (Add_Picture_Activity.this , Selecting_Activity.class);
editor = pref.edit() ;
editor.putString("IMAGEPATH" , picturePath);
editor.apply();
startActivity(sendingPicPath);
}
on other activity
String imagePath = user_image.getString("IMAGEPATH" ,"");
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
I'm developing an app in which one of its features is to allow the user to input a phone number or select one from the phones contacts.
The activity has 3 EditTexts along with 3 Buttons each on the side, and the buttons open the Contacts Activity and return the selected phone number and sets the EditText accordingly.
I have tried setting separate cursors for each button.
I have also tried using different intent variables.
My problem: When the user selects the contact to get data from, the cursor returns a different number. Either it returns the contact next to it or the contact previously selected.
The OnClick method for the 3 buttons with startActivityForResult.
//The intent
i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
public void onClick(View v) {
switch (v.getId()){
case (R.id.button1):
startActivityForResult(i, 0);
break;
//I know it's an ImageView
case (R.id.imageView1):
startActivityForResult(i, 1);
break;
case (R.id.imageView2):
startActivityForResult(i, 2);
break;
}
}
The OnActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode){
case (0) :
if (resultCode == Activity.RESULT_OK) {
Cursor c = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
c.moveToFirst();
contact1 = c.getString(c.getColumnIndex(Phone.NUMBER));
et.setText(contact1);
}
break;
case (1) :
if (resultCode == Activity.RESULT_OK) {
Cursor c1 = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
c1.moveToFirst();
contact2 = c1.getString(c1.getColumnIndex(Phone.NUMBER));
et2.setText(contact2);
}
break;
case (2) :
if (resultCode == Activity.RESULT_OK) {
Cursor c2 = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
c2.moveToFirst();
contact3 = c2.getString(c2.getColumnIndex(Phone.NUMBER));
et3.setText(contact3);
}
break;
}
}
change Phone.CONTENT_URI to intent.getData();
Uri contactData = intent.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
what you are doing wrong is querying each time the exact same query without regard to the intent result...so of course you will get the same result each time
Also because you want to get the phone number add a projection value:
String[] projection = {Phone.NUMBER};
getContentResolver().query(contactData, projection, null, null, null);
to better understand please read how to get contact phone number
When I tried to call multiple startactivityforResult in Android, it only called the first declared on startactivityforResult, I resolved this issue by making a function with startActivityforResult and passing the intent and requestcode to the function that contained startActivityForResult.
Want to set the value RESULT from the following part and should retrieve it in the onActivityResult...
Following is the code.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
System.out.println("Select Display Picture, but");
intent.putExtra("RESULT", "RESULT");
activity.startActivityForResult(
Intent.createChooser(intent, "Select Display Picture"),
Credentials.BROWSE_PIC);
activity.setResult(Credentials.BROWSE_PIC, intent);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Credentials.BROWSE_PIC
&& resultCode == Activity.RESULT_OK && null != data) {
//returning null always here..
System.out.println("OnActivityResult came in::: "
+ data.getStringExtra("RESULT"));
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
You are using implicit Intent, you can not put anything in this intent because every implicit intent is defined by others.
If you want to add something then you can use your own Global Bundle object for the same.
Here are important link for you:
You can see answer By Lavekush Agrawer for using Global Bundle Object. here access the variable in activity in another class
Android Intents - Tutorial
I have an app that among other things is trying to set a specific sound to an individual contact. Everything works, it shows the sound as the ringtone for the contact when you view the contact info but when the contact calls, the default ringtone rings. Can anyone shed any light on what's wrong?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Uri contactData = data.getData();
String contactId = contactData.getLastPathSegment();
Cursor localCursor = managedQuery(contactData, null, null, null, null);
localCursor.moveToFirst();
String str1 = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
String str2 = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
ContentValues localContentValues = new ContentValues();
localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, ringtonePath);
getContentResolver().update(localUri, localContentValues, null, null);
Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();
break;
}
In case anybody stumbles across this wondering how to achieve this, I used parts of the ringdroid library to assign a tone to a contact
https://github.com/google/ringdroid