I my app a user can create or choose like this:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME,
user.getName());
intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED,
true);
startActivityForResult(intent, ADD_CONTACT);
Then in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
Log.e(TAG, "DATA NULL");
return;
}
if (requestCode == ADD_CONTACT) {
if (resultCode == RESULT_OK) {
//QUESTION HERE
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null,
null, null);
} else {
Log.e(TAG, "RESULT NOT OK!");
}
}
}
Question: Is there any way to decide, if user created or chose an existing contact ? :)
You can get the number of contacts before user action and after it (with this: how many contacts in contact list). Than just compare.
You can keep count of the contacts in case you dont need the information regarding the change/added contact.
In case you need what has changed/added you can check for this answer for observer
Hope this helps
Related
UPDATE:
I have restarted my device, and it works now. Like a magic!!
ORIGINAL QUESTION:
I have read many answers for my question, but i still can't get a solution for my problem:
I had 1 fragment that opened an intent for capturing a photo and in the fragment I had the method OnActivityResults and all worked fine.
Now, I added 2nd fragment that also calls an intent with the same code (but different request code). I'm not sure that this cause the problem, but now, when I push the "V" that approve the captured photo, I'm getting back to different fragment and the OnActivityResults method isn't called.
In the fragment:
private static final int REQUEST_TAKE_PHOTO_CODE = 11;
private static final int REQUEST_ATTACH_PHOTO_CODE = 22;
takePhotoButton = (ImageButton)rootView.findViewById(R.id.imageButtonTakePhoto);
takePhotoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MainActivity.deviceHasCamera){
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO_CODE );
}
else{
Toast.makeText(activity, "No camera detected", Toast.LENGTH_SHORT).show();
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("EB", "onActivityResult CarAccident");
switch (requestCode) {
case REQUEST_TAKE_PHOTO_CODE:
//This case is when the user decide to Approve the captured photo
if (resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Log.d("EB", "BitMap = " + photo.toString());
}
break;
case REQUEST_ATTACH_PHOTO_CODE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Log.d("EB", "Uri fata.getData() = " + data.getData().toString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = this.activity.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
photo = BitmapFactory.decodeFile(filePath);
//For the case that is Android 5.0 and the photo is on the server
// and not on the device
if (photo == null){
try {
photo = getBitmapFromUri(data.getData());
Log.d("EB", "photo = getBitmapFromUri(data.getData()) = " + photo.toString());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this.activity, FAILD_TO_ATTACH_PHOTO_MESSAGE,
Toast.LENGTH_SHORT).show();
}
}
//Set the photo to the imageView
imageView.setImageBitmap(photo);
Log.d("EB", "attached image = " + ((photo != null) ? photo.toString() : "NULL"));
}
break;
}
}
I tried to to write in the host Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
But it's not working. Hope you can help.
Thanks in advance!
You cant do this.
You must call it in your Activity and check if the result true with REQUEST_CODE. If equeals with your code, you must retrieve your data from method and do what you need. This is origin solution.
i am starting contact app using intent and selecting the contact from the list each time i choose different contact but i am getting the same contact again and again and i also don't know which contact it is? I have no idea what is going on?
Here is my code
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
and in activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Cursor c = null;
try
{
if(requestCode == PICK_CONTACT)
{
Uri contactData = data.getData();
c = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
if (c.moveToFirst())
{
reciever = c.getString(c.getColumnIndex(Phone.NUMBER));
reciever = Main.removeCharacters(reciever);
int size = reciever.length();
Log.v(TAG, "To send "+reciever);
reciever = reciever.substring(size - 6, size);
}
new UploadPic().execute("");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(c != null)
{
c.close();
Log.v(TAG, "Cursor Closed");
}
}
}
You are assigning the contact URI to contactData yet you do not use it. When you call query() pass in contactData for the first argument rather than Phone.CONTENT_URI.
I'm using an intent like this:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
And in onActivityResult() I have this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return; // user cancelled
}
Uri imageUri = data.getData();
if (imageUri == null) {
// (code to show error message goes here)
return;
}
// Get image path from media store
String[] filePathColumn = { android.provider.MediaStore.MediaColumns.DATA };
Cursor cursor = this.getContentResolver().query(imageUri, filePathColumn,
null, null, null);
if (cursor == null || !cursor.moveToFirst()) {
// (code to show error message goes here)
return;
}
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
if (imagePath == null) {
// error happens here
}
}
When I select images from particular albums like "Posts", "Profile Photos" (see screenshot) I'm unable to get the image path in onActivityResult(). Images from other albums can be selected with no problems.
I've tried adding intent.putExtra("return-data", true) but data.getExtras() returns null in onActivityResult().
There is similar question here, but no one answered it.
Please help!
hops this will helps you ....
ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
BitmapFactory.Options options = new BitmapFactory.Options();
final InputStream ist = ontext.getContentResolver().openInputStream(intent.getData());
final Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
ist.close();
}
}
if above code doesn't work than just refer this link... it will surly shows the way
http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
try this:
String selectedImagePath = imageUri.getEncodedPath();
it works for me using gallery image picker
maybe this:
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
This is my first strange result that I never expected. This is may be lack of skill in that area.
Well I had a button, through which I need to select an image from the phone gallery (probably from sdcard). I used implicit intent to call the phone gallery and got the absolute image path with startActivityForResult(). Immediately am calling another activity putting that path with startActivity().
According to my scenario I wrote the following code in the onClick() of button.
#Override
public void onClick(View v) {
upLoadPhoto();
}
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
System.out.println("select image");
startActivityForResult(intent, 1);
startActivity(next);
finish();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && data != null && data.getData() != null){
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
System.out.println("Background : "+imageFilePath);
next.putExtra("backImagePath", imageFilePath);
cursor.close();
super.onActivityResult(requestCode, resultCode, data);
}
}
}
When I click the button, startActivity(next) is called first, then startActivityForResult(intent,1) is called. As am trying to get image path in the second activity through bundle object, am getting NullPointerException because of startActivity(next) is being called first.
I dropped my jaws when I saw my debugging point are not as expected. Hope I get exact reason to this issue.
Thanks
Aswin
What about moving the call to startActivity(next); into onActivityResult()... this way you will navigate to the other activity after getting the path
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
System.out.println("select image");
startActivityForResult(intent, 1);
finish();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && data != null && data.getData() != null){
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
System.out.println("Background : "+imageFilePath);
next.putExtra("backImagePath", imageFilePath);
cursor.close();
startActivity(next);
super.onActivityResult(requestCode, resultCode, data);
}
}
}
You have call startActivity() inside onActivityResult() when your image is selected from Gallery successful. Because when startActivityForResult() it is going to Gallery for picking up the image and till then startActivity() is fired and you move to next Activity.
remove that startActivity(next) from that function and put it on onActivityResult after you retrieve your data
remove that startActivity(next) from that function and put it on onActivityResult after get 1 add Your Next Intent
I have the contact and I would like the user to choose from his phones or emails. Is that already provided by some picker or shall I implement my own?
Try this
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONTACT_PICKER_RESULT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
editTextRecipient.setText(name);
}
}
break;
}
}