I am working on one sample application just to insert, update and delete the native android contact. I am able to successfully insert, update and delete the contact. But the problem in updating the contact photo. Below images are the observation where the same contact having two different issue.
After updating the contact, first image is still displaying the old image. But where as when i view the full details i am able to view the newly updated contact image as shown in the second image. Below is the code for updating the contact image.
mBitmap =getAllowedPhotoBitmap(photo);
mBitmap = ThumbnailUtils.extractThumbnail(mBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(mBitmap!=null){ // If an image is selected successfully
mBitmap.compress(Bitmap.CompressFormat.PNG ,100, stream);
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
op.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[{String.valueOf(native_contactid), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
op.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, stream.toByteArray());
ops.add(op.build());
}
What is the problem and where i am going wrong?
Here is a open source app that does that: https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java
this file will help you set Image for contact with Contact ID
https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java
void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple
if (requestCode == PICK_CONTACT_REQUEST) {
Uri contactUri = data.getData();
String[] projection = {Phone.CONTACT_ID,Phone.NUMBER,ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.Data._ID };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int columcontactID = cursor.getColumnIndex(Phone.CONTACT_ID);
String contactID = cursor.getString(columcontactID);
Bitmap item = (imgBg.getVisibleRectangleBitmap());
setContactPicture(AtWallpaperDetails.this, contactID, item);
}
}
}
Related
my goal is to retrieve a contact's phone number after it gets picked in the contact list. This is the code I am using
private void onClick(){
Intent pickContact = new Intent(Intent.ACTION_PICK);
pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContact, CONTACT_PICK_CODE);
}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == CONTACT_PICK_CODE) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(phoneIndex);
}
c.close();
}
}
which works fine if the standard Android contacts app is being used, but crashes when the app "Simple Contacts" is being used.
In particular, the error happens because phoneIndex is -1.
How to make it work even if the user chooses to use another contact list app?
For reference, this is the cursor when the contact gets selected from Simple Contacts.
As you can see it contains only 36 columns, while using the standard contact list app it has 84 columns, one of which is ContactsContract.CommonDataKinds.Phone.NUMBER
I'm using an image picker intent, to allow users to choose an image from their gallery, I get it's path, and pass it then to a 3rd library.
It's working fine for most of the cases, but if I picked up an image from Google Photos (an image that is stored online) I get a null path, though that I get valid URI for both of the working and not working images.
Here is my Intent call:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and here is onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
Log.e(getClass().getName(),"file uri = " + uri);
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection,
null, null, null);
if(cursor == null) return;
Log.e(getClass().getName(),"file cursor = " + cursor);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e(getClass().getName(),"file columnIndex = " + columnIndex);
cursor.moveToFirst();
// The crash happens here
String photoPath = cursor.getString(columnIndex);
Log.e(getClass().getName(),"file photo path = " + photoPath);
cursor.close();
cropImage(photoPath);
}
And here are the logs for working and not-working image:
Working image:
file uri = content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F105681/ORIGINAL/NONE/187859359
file cursor =
android.content.ContentResolver$CursorWrapperInner#8953964
file columnIndex = 0
file photo path = /storage/emulated/0/DCIM/Camera/IMG_20190523_184830.jpg
Not working image:
file uri =
content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253A4574915c-b4ac-40af-bc08-b1004670cab2/ORIGINAL/NONE/477302338
file cursor =
android.content.ContentResolver$CursorWrapperInner#59448a4
file columnIndex = 0
file photo path = null
If there isn't way to avoid that error, is there a way instead to hide photos that are stored online and only show local ones?
The technique in your question has (at least) three problems:
Not every MediaStore entry has a value for DATA, as you're seeing
Not every non-null DATA value represents a filesystem path that you can access, as the MediaStore can get to content that you can't
The DATA column is not available on Android Q and higher
In your case, the uCrop library accepts a Uri. Well-written Android libraries know how to handle a Uri, so you can just hand the Uri over to the library and it will take it from there.
In my project i need a way of get the path of several images selecteds from the gallery.
I´m using this library MultipleSelectImages
It´s apparently work fine, but in the onActivityResult i need the array with the path of each image, however the result I get is this:
Paths: [com.darsh.multipleimageselect.models.Image#1e6d3057, com.darsh.multipleimageselect.models.Image#33824744]
...when i need the real path (/storage/emulated/0/DCIM/Camera/20150426_110936.jpg)
Reading the doc of the library don´t found solution.
This is the onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
Log.i("myLogs", "Paths:" + " " + images);
}
}
...Where "image" it´s imported from the library
import com.darsh.multipleimageselect.models.Image;
I´m not using EXTRA_ALLOW_MULTIPLE because need that the app works in android api 16 version
Thanks in advanced.
Try to use below code;
onActivityResult();-
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 imgPath = cursor.getString(columnIndex);
cursor.close();
I want to retrieve the mobile number from the contacts (I want to send an sms). Here my code:
//Selecting the contact
Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, RQS_PICK_CONTACT);
}});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Cursor cursor = null;
mName.setText(context.getString(R.string.not_available));
mNumber.setText(context.getString(R.string.not_available));
if(requestCode == RQS_PICK_CONTACT && resultCode == RESULT_OK && data != null){
Log.d(TAG, "requestCode, resultCode, data ok");
Uri uri = data.getData();
try{
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER};
// cursor = getContentResolver().query(uri, projection, null, null, null);
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
Log.d(TAG, "Trying to retrieve the name and the number");
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String hasNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));
Log.d(TAG, "hasNumber "+hasNumber);
mName.setText(name);
if(hasNumber.trim().equals("1")){
Log.d(TAG, "contact has telephone number");
//set name and number
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mNumber.setText(phoneNumber);
}
}catch(Exception ex){
CharSequence text = context.getString(R.string.cannot_choose_contact);
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
if(cursor!= null && !cursor.isClosed()){
cursor.close();
}
}else{
CharSequence text = context.getString(R.string.cannot_choose_contact);
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
I am getting: Failed to read row 0, column -1 from CursorWindow...
How do I get the phone number - am I trying to retrieve it from the right column?
Thanks in advance for your answers,
The detailed data for a contact is contained in a separate table from the main contact itself (see the Contacts API guide for more detail). Since you're sending an SMS, it might be more useful to only get the contacts who have a phone number associated, so you might as well go straight for the table which contains phone numbers. For the URI, I use:
CommonDataKinds.Phone.CONTENT_URI
Then you don't have to worry about HAS_PHONE_NUMBER. At a glance, the rest of your code looks right or very close. If you wanted to continue down your original path, you'd have to do a separate query on this table anyway, but provide it the ID of the contact that you initially found.
Use a CursorLoader to do queries. Always. If you continue to do queries on the UI thread, eventually you'll run into a situation where you hang the system and get an ANR.
You're requesting the user to pick contacts from the Contacts table, so you'll get back a URI that points to a contact in Contacts.
A trick for handling this is to strip the contact's LOOKUP_KEY from the returned URI using
Uri.getLastPathSegment(). Then search ContactsContract.Data for the LOOKUP_KEY and the
MIMETYPE value CommonDataKinds.Email.CONTENT_ITEM_TYPE. Based on your code, this would be:
mLookupKey = uri.getLastPathSegment();
String SELECTION = Data.LOOKUP_KEY + " = ? " +
" AND " +
Data.MIMETYPE + " = ?";
String[] selectionArgs = {
mLookupKey,
Email.CONTENT_ITEM_TYPE
};
...
i am facing an issue of taking picture from android camera and then showing it to next activity. process works fine but out of 10 pictures 1 picture is missed.
steps: activity a where i invoke camera and take picture then i pass to activity b of its URI to display in image view , repeating this cycle 10 times 1-2 times picture got missed and delay is observed of 2 sec when picture is taken and displayed into image view on next screen in the form of blank screen.
kindly check the code and guide me what mistake am i doing here?
Activity A
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
Cursor myCursor = this.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try{
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}
finally{myCursor.close();}
//Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";
try{
myCursor.moveToFirst();
//This will actually give you the file path location of the image.
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
}
finally{myCursor.close();}
// These are the two URI's you'll be interested in. They give you a handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
Intent next= new Intent(this,AddListing.class);
next.putExtra("imageUriThumb", uriThumbnailImage.toString());
next.putExtra("imageUriFull", uriLargeImage.toString());
startActivity(next);
}
}
Activity B
String uri = bundle.getString("imageUriThumb");
Uri myThumbUri = Uri.parse(uri);
try {
tempBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myThumbUri);
imgThumbnail.setImageBitmap(tempBitmap);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
tempBitmap.compress(Bitmap.CompressFormat.PNG, 90, bao);
byte [] ba = bao.toByteArray();
imageBytesString = Base64.encodeToString(ba, 0);
} catch (Exception e) {
}
I ended up this problem with following points:
i have searched this issue on internet already actually these phones dont get images using intent.get("data") and that's why i used above mentioned technique. i have tried this using Samsung ace phone , Samsung Galaxy and same problem is there. i guess whole Samsung dont work perfectly and delayed response is there when you have requirement of taking pictures again and again :( rest of phones like htc works perfect.