OnActivityResults in fragment not called - android

UPDATE:
I have restarted my device, and it works now. Like a magic!!
ORIGINAL QUESTION:
I have read many answers for my question, but i still can't get a solution for my problem:
I had 1 fragment that opened an intent for capturing a photo and in the fragment I had the method OnActivityResults and all worked fine.
Now, I added 2nd fragment that also calls an intent with the same code (but different request code). I'm not sure that this cause the problem, but now, when I push the "V" that approve the captured photo, I'm getting back to different fragment and the OnActivityResults method isn't called.
In the fragment:
private static final int REQUEST_TAKE_PHOTO_CODE = 11;
private static final int REQUEST_ATTACH_PHOTO_CODE = 22;
takePhotoButton = (ImageButton)rootView.findViewById(R.id.imageButtonTakePhoto);
takePhotoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MainActivity.deviceHasCamera){
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoIntent, REQUEST_TAKE_PHOTO_CODE );
}
else{
Toast.makeText(activity, "No camera detected", Toast.LENGTH_SHORT).show();
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("EB", "onActivityResult CarAccident");
switch (requestCode) {
case REQUEST_TAKE_PHOTO_CODE:
//This case is when the user decide to Approve the captured photo
if (resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Log.d("EB", "BitMap = " + photo.toString());
}
break;
case REQUEST_ATTACH_PHOTO_CODE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Log.d("EB", "Uri fata.getData() = " + data.getData().toString());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = this.activity.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
photo = BitmapFactory.decodeFile(filePath);
//For the case that is Android 5.0 and the photo is on the server
// and not on the device
if (photo == null){
try {
photo = getBitmapFromUri(data.getData());
Log.d("EB", "photo = getBitmapFromUri(data.getData()) = " + photo.toString());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this.activity, FAILD_TO_ATTACH_PHOTO_MESSAGE,
Toast.LENGTH_SHORT).show();
}
}
//Set the photo to the imageView
imageView.setImageBitmap(photo);
Log.d("EB", "attached image = " + ((photo != null) ? photo.toString() : "NULL"));
}
break;
}
}
I tried to to write in the host Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
But it's not working. Hope you can help.
Thanks in advance!

You cant do this.
You must call it in your Activity and check if the result true with REQUEST_CODE. If equeals with your code, you must retrieve your data from method and do what you need. This is origin solution.

Related

Handle the Camera in a Fragment

Excuse me for any grammatical errors.
I would like to use the camera in a different activity than mainActivity.
I found this simple tutorial on google Taking Photos Simply, that says I have to use this function:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
But, this function works fine only into the MainActivity, in fact, if I put this code in a different activity I get this error:
Cannot resolve method 'getPackageManager()'.
Some ideas?
Thank you!
Try out the following code:
static final int REQUEST_IMAGE_CAPTURE = 1;
Context c;
private void dispatchTakePictureIntent() {
Fragment yourFragment = this;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
yourFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Override your parent Activity's onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Then add this to your fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
//Do something with your captured image. EX:-
try {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Excuse me, I don't know why, when I try this application on my Nexus 5X, the photo lose the quality when it appears into the ImageView. Application image (Image View): i.imgur.com/VunFaHF.png?1 Camera Image: i.imgur.com/wjJ2a4w.jpg

import images from gallery into fragment

im trying to implement a fragment inside a navigation slider. I need to create a button in 1 of my fragments to import images from my default gallery. I have tried many codes online, but they don't seem to be working.
It depend on android version you are using if you test your app in android 6.0 than you should ask permission at runtime else image is not returned by android.
For Pre-Marshmallow you can use code:
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 0);
and then override method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, requestCode, data);
try {
// When an Image is picked
if (requestCode == 0 && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
image.setImageBitmap(BitmapFactory.decodeFile(getRealPathFromURI(selectedImage)));
} else
new ShowErrorToast(getActivity(), "Hey! your Android phone is busy");
} catch (Exception e) {
}
}
than use method to get path of image
public String getRealPathFromURI(Uri data) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(data
, filePathColumn, null, null, null);
String picturePath = "";
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}
return picturePath;
}
For Marshmallow you should ask for permission runtime and follow above code

Unable to select particular images using ACTION_PICK intent

I'm using an intent like this:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
And in onActivityResult() I have this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return; // user cancelled
}
Uri imageUri = data.getData();
if (imageUri == null) {
// (code to show error message goes here)
return;
}
// Get image path from media store
String[] filePathColumn = { android.provider.MediaStore.MediaColumns.DATA };
Cursor cursor = this.getContentResolver().query(imageUri, filePathColumn,
null, null, null);
if (cursor == null || !cursor.moveToFirst()) {
// (code to show error message goes here)
return;
}
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
if (imagePath == null) {
// error happens here
}
}
When I select images from particular albums like "Posts", "Profile Photos" (see screenshot) I'm unable to get the image path in onActivityResult(). Images from other albums can be selected with no problems.
I've tried adding intent.putExtra("return-data", true) but data.getExtras() returns null in onActivityResult().
There is similar question here, but no one answered it.
Please help!
hops this will helps you ....
ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(intent, requestCode);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
BitmapFactory.Options options = new BitmapFactory.Options();
final InputStream ist = ontext.getContentResolver().openInputStream(intent.getData());
final Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
ist.close();
}
}
if above code doesn't work than just refer this link... it will surly shows the way
http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/
try this:
String selectedImagePath = imageUri.getEncodedPath();
it works for me using gallery image picker
maybe this:
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());

How to use onConfigurationChanged in case of Gallery intent?

In my application I am giving the user an option to choose a picture from gallery or click it from a camera, using the inbuilt camera and gallery features and setting intents for the same. Somehow on Orientation change I lose the data passed from these intents. After looking up I came to know that orientation change causes activity to redraw and re-initialize. Now I want to use Bundle and OnConfigurationChange and save my extras. I am clueless about how to achieve it, a detailed answer could be helpful. Below is my code:
if(i==0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == 1337 && resultCode== Activity.RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null){
TitmapFactory.Options options = new TitmapFactory.Options();
options.inSampleSize = 1;
options.inPurgeable = true;
options.inInputShareable = true;
thumbnail = (Titmap) data.getExtras().get("data");
// imgview.setImageTitmap(thumbnail);
image(thumbnail);
} else {
Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
if (requestCode == RESULT_LOAD_IMAGE && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
thumbnail = (BitmapFactory.decodeFile(picturePath));
image(thumbnail);
}

Large image Extra in Intent, cause black Screen in Android

I have this code to pick the images from gallery or camera:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
ext = filePath
.substring(filePath.lastIndexOf(".") + 1);
photod = BitmapFactory.decodeFile(filePath);
new AsyncTaskOne().execute(new String[] {});
(fileNameIndex);
}
}
break;
case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
if (requestCode == CAMERA_REQUEST) {
photod = (Bitmap) data.getExtras().get("data");
new AsyncTaskOne().execute(new String[] {});
}
}
}
When I press the confirm button I invoke this listener
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};
(where codedPhoto is the image coded in base64 by another method)
to send the image and its extension in another activity
Everything works fine with small images but if I chose a medium size image or large photo(also if isn't very large), the app freezes, the screen becomes black and If I wait some minutes return on the current activity without showing any error in the stack and the invoked intent PropertiesUpdatedPhoto doesn't start.
How I could fix this problem?
This problem is discuss here, we should keep intent extra content as small as possible.
My suggestion is instead of passing image, perhaps passing the image URI or Path would be a better solution. Then only load the image in that Activity.
Example :
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
// Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};

Categories

Resources