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.
Related
Here is my On Click Listener:
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PHOTO_PICKER_REQUEST_CODE);
}
here is my OnActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageUri));
long length = imageFile.length();
length = length / 1024;
Log.d(TAG, "onActivityResult: ImageSize " + length +" Kb");
}
}
ImageSize always returns 0 (zero).
You probably missing the permission in the manifest. You need to add the following permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Or, if you're targetting Android 6.0 (API level 23) and above, you need to Requesting Permissions at Run Time
And please check your getRealPathFromURI() method. I've testing with this following code and it's work:
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, "", null, "");
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
Or use the code from Convert content:// URI to actual path in Android 4.4
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/
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();
}
}
}
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
I'm trying to save the path of an image imported from the gallery using this method:
case R.id.media:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
return true;
Here is the on activity result method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final 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 picturePath = (cursor.getString(columnIndex));
cursor.close();
mImage = picturePath;
ImageView imageView = (ImageView) findViewById(R.id.note_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent viewImageIntent = new Intent(android.content.Intent.ACTION_VIEW, selectedImage);
startActivity(viewImageIntent);
}
});
}
}
And here is the populate field method:
mImage =(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_IMAGE)));
But this is not working, the path doesnt get saved and when i close the activity and start the activity again, the image is gone. How can i fix this?