Getting Android contacts information - java

I'm trying to get a contacts name from my contacts by using an Intent, it seems the method I found earlier is deprecated and when I try to use it my app force quits. I'm new to Android development and any help would be appreciated.
public void openContacts(View view) {
Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactsIntent, 1);
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
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(Contacts.People.NAME));
TextView contactName = (TextView)findViewById(R.id.contact_name_textview);
contactName.setText(name);
}
}
}
}
I'm calling the openContacts function as an onClick event for a button. Once I've picked my contact I would like to set the name to a TextView that I have already made.

Related

Choose a contact and fill it in EditText

I develop an app in android and I want when to press a button opens the contacts and choose one name and fill that name in EditText .. just like the SMS message.
here my code it just open the contact and when I choose the name it's going nowhere!
addto.setOnClickListener(new View.OnClickListener() {
private static final int PICK_CONTACT = 0;
#Override
public void onClick(View v) {
Intent it= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(it, PICK_CONTACT);
}
});
You can get contact information in onActivityResult() method inside your activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICK_CONTACT:
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
//Get number and name from cursor
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//set number and name in editext
etName.setText(contactName);
etNumber.setText(number);
break;
}
}
}

how can i delete a contact after choosing it in my intent android

How can I delete a contact after choosing it in my intent android?
I tried searching but I couldn't find anything, thanks in advance!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) !=null)
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==RESULT_OK){
Uri contactUri = data.getData();
}
}
You have to use ContentResolver & Cursor Query to delete particular contact.Below Usage :
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
try
{
cr.delete(contactUri, null, null);
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}

Get all the images from the Gallery in Android

I want to make my own Gallery in Android but I canĀ“t find the way to bring all the photos without selecting them. I have tried this:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
But opens an intent in which I have to select one or more photos. Is there a way I can bring them all without selecting them?
This is a sample code for it
public class GetImageActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
For your question hell of solutions is available in SOF. So before posting a new question google it first.look into these examples if you face any problem..
android gallery into grid style menu
GridView loading photos from SD Card
How to implement Image Gallery in Gridview in android?
Android-Fetch images from sdcard and display in gridview

How to get group of contacts list from Phone Contacts

btngal.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(Intent.ACTION_PICK,android.provider.ContactsContract.Contacts.CONTENT_GROUP_URI);
startActivityForResult(i,0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
if (requestCode == 0) {
Uri selectContact=data.getData();
String[] filePathColumn={Contacts.CONTENT_URI.toString()};
Cursor cursor = getContentResolver().query(selectContact,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}
}
}
While doing this action I am getting below error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.PICK dat=content://com.android.contacts/contacts/group }
In order to read contacts from native android app, you need permission, like this in your manifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Make sure you have that one.

Get image Uri in onActivityResult after taking photo?

I have this code:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_IMAGE);
That allows that user to take a photo. Now how would I get the Uri of that photo in onActivityResult? Is it an Intent extra? Is it through Intent.getData()?
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
Uri u = intent.getData();
}
By the way... there's a bug with that intent in some devices. Take a look at this answer to know how to workaround it.
Instead of just launching the intent, also make sure to tell the intent where you want the photo.
Uri uri = Uri.parse("file://somewhere_that_you_choose");
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(photoIntent, CAMERA_IMAGE);
Then when you get your onActivityResult() method called, if it was a success just open a stream to the URI and it should all be set.
Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
uri = data.getData();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri fileUri = Utils.getUri(getActivity(), photo);
}
}
public String getRealPathFromURI (Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}

Categories

Resources