Unable to read data from contact picker - java

I am unable to read and handle the data of contact from my activity.
By executing my code , it displays a contact picker but when i select a contact it shows a dialog box as "Unfortunately YourApp has been stopped".
I am able to choose the contact but can't read the data as phone number,name etc.
I am quite sure that there is some mistake in onActivityResult() method
The code in onActivityResult() method is:-
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
contact=data.getData();
String projection[]={Phone.NUMBER};
Cursor c=getContentResolver().query(contact, projection, null, null, null);
c.moveToFirst();
int column=c.getColumnIndex(Phone.NUMBER);
String number=c.getString(column);
Toast.makeText(this,"The number of selected contact is:-"+ number, Toast.LENGTH_LONG).show();
}
}
Please help me.
I am new to android programming.
Thanks in advance.

I think your onActivityResult should be like below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Uri result = data.getData();
String id = result.getLastPathSegment();
String projection[] = { Phone.NUMBER };
Cursor c = getContentResolver().query(Phone.CONTENT_URI, projection,
Phone.CONTACT_ID + "=?", new String[] { id }, null);
c.moveToFirst();
int column = c.getColumnIndex(Phone.NUMBER);
String number = c.getString(column);
Toast.makeText(this,
"The number of selected contact is:-" + number,
Toast.LENGTH_LONG).show();
}
}
}

Related

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

Restrict selecting file above some size in Android Studio

I a beginner in android, I want to select an image or a video file. But I want to restrict the selected file size, let's assume the restriction size is 5MB. Above 5MB I want to not allow the user to select the file.
How can I do it?
Any help would be appreciated.
Button OnClick:
btnSelectFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, 21);
}
});
OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 21 && resultCode == RESULT_OK) {
// Get the Uri of the selected file
uri = data.getData();
uriString = uri.toString();
myFile = new File(uriString);
path = myFile.getAbsolutePath();
displayName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
Log.e("TAG","Size: "+Long.toString(cursor.getLong(sizeIndex)));
textFileSelected.setText(displayName);
fileName = textFileSelected.getText().toString();
Toast.makeText(this, "File Selected: " + fileName, Toast.LENGTH_SHORT).show();
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
textFileSelected.setText(displayName);
fileName = textFileSelected.getText().toString();
Toast.makeText(this, "File Selected: " + fileName, Toast.LENGTH_SHORT).show();
}
}
}

For what this Android code is used?

I'm new on Android development, can you please explain me the following code and for what this Android code is it used?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == getActivity().RESULT_OK
&& null != data) {
selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
_txt_img_url.setText(fileName);
Toast.makeText(getActivity(), fileName, Toast.LENGTH_SHORT).show();
ImageTask imgtask = new ImageTask();
imgtask.execute();
onActivityResult method is used to handle the data returned from an intent,
check here the documentation
https://developer.android.com/training/basics/intents/result.html
In short in this case if the result is ok, and we have data to handle, a toast appear with the path of the selected file.

Get contact name?

I want to get the contact name, but I'm not able to. After looking at this answer, I tried to get the name using family, given, and display, but nothing worked
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst(); //Move to first row...I actually dont know why this part is necessary, but I get an error without it...
int NumberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); //Int column is the column of the numbers
int NameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
String contactNumber = cursor.getString(NumberColumn);
String contactName = cursor.getString(NameColumn);
Toast.makeText(MainActivity.this, ""+ contactNumber +"" +contactName, Toast.LENGTH_SHORT).show();
}
/
public void addContact(View v){ //OnClick listener to launch contact picker
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
Try below code for getting contact of specific number
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactName();
}
}
private void retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
}
More detail refer below link https://tausiq.wordpress.com/2012/08/23/android-get-contact-details-id-name-phone-photo/

Retrieve data from onActivityResult (Fragment class)

onActivityResult (Activity class)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
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]);
Data.desiredaddress = cursor.getString(columnIndex);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
onActivityResult (Fragment class)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor1 = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor1.moveToFirst();
int columnIndex = cursor1.getColumnIndex(filePathColumn[0]);
Data.desiredaddress = cursor1.getString(columnIndex);
cursor1.close();
}
}
My problem :
The value of Data.desiredaddress change if it is modify in activity class but it did not change in fragment class.
can someone help me with this.
the app has no error and it didn't crash.
Thank you.
After i go through some other question , i found out that the reason for this is because onActivityResult (parent activity) will not be ignored even if i called onActivityResult from fragment class.
Thank you to #Aswin Rajendiran for the idea.
here is the link for further explaination : https://stackoverflow.com/a/16434914/4148255

Categories