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/
Related
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();
}
}
}
I'm trying to retrieve the actual image name of the image selected by user in gallery(Ex. IMG_2020).
I tried using getAbsolutePath() and getName() but these 2 methods display something like "image$3A75" instead of the actual image file name.
Other than that, I also tried using some cursor method which I don't know really how it works, but it still doesn't work or maybe it's because I do not know how to use it.
Any help please?
This is my onActivityResult() method
fileName is the textView I want to place my imageName
bitMap object is not used yet because I'm following a tutorial online and I'm halfway copying, I think it will be used later in the tutorial
public class createCharity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
Button uploadButton;
Button createCharityButton;
TextView charityTitle;
TextView fileName;
TextView charityDescription;
Uri charityImage;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_charity);
uploadButton = findViewById(R.id.uploadButton);
charityTitle = findViewById(R.id.charityTitle);
fileName = findViewById(R.id.fileName);
charityDescription = findViewById(R.id.charityDescription);
createCharityButton = findViewById(R.id.createCharityButton);
mStorageRef = FirebaseStorage.getInstance().getReference("charityUploads");
mDatabaseRef = FirebaseDatabase.getInstance().getReference("charityUploads");
uploadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFileChooser();
}
});
}
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
System.out.println("----------------------c1");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
charityImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), charityImage);
File file = new File(String.valueOf(charityImage));
System.out.println("Image name: " + file.getName());
fileName.setText(file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Something like:
String projection [] = {
MediaStore.Images.Media.DATA
, MediaStore.Images.Media.DISPLAY_NAME
, MediaStore.Images.Media.SIZE};
Cursor cursor = getContentResolver().query(data.getData(), projection, null, null, null);
if ( cursor==null)
{
Toast.makeText(context, "cursor==null\n\ncould not query content resolver for\n\n" + path, Toast.LENGTH_LONG).show();
return;
}
cursor.moveToFirst();
String data = cursor.getString(0);
String displayName = cursor.getString(1);
String size = cursor.getString(2);
Toast.makeText(context, "getContentResolver().openInputStream() ok\n\n" + path
+ "\n\nDISPLAY_NAME: " + displayName
+ "\nDATA: " + data
+ "\nSIZE: " + size
, Toast.LENGTH_LONG).show();
cursor.close();
DATA not available on Android Q+.
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.
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();
}
}
}
I have been trying to figure out a way for my application to open a file browser where the user can select a video File and have my app store that path into a string to use later. Is there an Intent I can use or function? Any suggestions?
You can do it via intent chooser. Try this code i think this solution solve your purpose.
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Set your required file type
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "DEMO"),1001);
then onActivityResult method.
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
Uri currFileURI = data.getData();
String path=currFileURI.getPath();
}}
Here is a code I have used one of my previous projects.
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video "), 33);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 33) {
Uri selectedMediaUri = data.getData();
filemanagerstring = selectedMediaUri.getPath();
selectedMediaPath = getPath(selectedMediaUri);
if (!selectedMediaPath.equals("")) {
filePath = selectedMediaPath;
} else if (!filemanagerstring.equals("")) {
filePath = filemanagerstring;
}
int lastIndex = filePath.lastIndexOf("/");
fileName = filePath.substring(lastIndex+1);
//filepath is your file's path
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return "";
}
use below code.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
and on activity result you get file path.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
String FilePath = data.getData().getPath();
textFile.setText(FilePath);
}
break;
}
}