onActivityResult() only triggers when Camera is cancelled - android

I'm having some trouble using the Camera intent in Android. I want to be able to take
a picture and to use the picture later (retreiving the URI). Here's the code:
newPicBut.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
PhotoChoiceActivity.this.launchCameraIntent();
}
});
public void launchCameraIntent()
{
//create camera intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//set fileUri
MediaManager manager = new MediaManager();
manager.setPictureName(currentItemToAdd.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, manager.getOutputMediaFileUri());
startActivityForResult(intent, 100);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 100){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show();
}
}
}
So basically the button calls the launchCameraIntent() method. The MediaManager creates a file, and I use the file URI in the intent.putExtra to indicate where the file should be saved. The Camera intent is called, but here is the problem: the onActivityResult() only gets triggered when I cancel my camera intent. So it doesn't get triggered at all when I accept the picture that I've taken.
Hope you can help!

Related

Activity always returns RESULT_CANCELLED despite manually setting the result as RESULT_OK

I am writing a component that allows user to pick a location based on the place indicated by the location picker. One of the requirements is to send the LatLng object back from the map activity to the activity that called the former. The problem is that returned result code is always RESULT_CANCELLED, despite setting it explicitly to RESULT_OK. Here's the code:
Calling activity:
public void getLocationBtn(View view) {
Intent i = new Intent(this, PickLocationActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
location = data.getParcelableExtra("location");
Log.d(TAG, "gotLocation: " + location);
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Location not chosen", Toast.LENGTH_SHORT).show();
}
}
}
Called activity:
btnFind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
centerLatLang = mMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
Button doneBtn = findViewById(R.id.locationPickerDoneBtn);
doneBtn.setEnabled(true);
}
});
}
public void doneBtn(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra("location", centerLatLang);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
btnFind get the coordinates, doneBtn confirms user's choice and comes back to the previous activity.
I have already tried replacing Intent returnIntent = new Intent(); with getIntent(), but it didn't work; the returned bundle was null.
it happen when your activity is using singleTask launch mode. so i recommand if you have below line in your manifest activity tab please remove it.
android:launchMode="singleInstance"

Take a picture and name it

I have built an Android application that allows the user to take pictures, I don't need to save the picture to the photo album but I want to give to picture a custom name and I don't know how to do.
This is how I open the camera:
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
and this is how I save the photo:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
DataManager.bars.get((Integer) getIntent().getSerializableExtra("bar")).images.add(thumbnail); // here I save the picture to my custom object
this.adapter.notifyDataSetChanged(); // I update the recycler view in order to see the pic
} else {
Toast.makeText(this, "Picture Not taken", Toast.LENGTH_LONG).show();
}
}

Camera onActivityResult on fragment

I'm working on some code someone else wrote and I'm having trouble handling the result of a camera intent.
Basically i have a DashBoardActivity which contains a tab with a fragment called "MyProfileContainer", which contains a "SettingsFragment" fragment which contains a "EditProfileFragment"fragment.
In the EditProfileFragment the user can take a picture for his profile. It works but it calls the onActionResult on the Dashboard Activity.
I read some guide on how to redirect it to the EditProfileFragment but I haven't been able to do it.
I'm losing literally days on this one and I can't figure it out.
This is onActivityResult on the Dashboard Activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
And this is EditProfileFragment
private Uri imageUri = null;
public void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getParentFragment().startActivityForResult(intent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
profilePhoto.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
I don't know if I have to override the method on every class between this two or if I'm doing something else wrong, but I'm sure the EditProfileFragment onActivityResult is never called.
I found out the problem, and it was actually a bug of Android.
The fragment that was to recieve the result was a fragment nested inside other fragment and the method call was not properly propagated that deeply.
I had to override the method on the containing fragment manually and it worked.
The easy trick to call OnActivityResult in nested fragment.
1) Add this code in your captureImage method it will start a new activity.
Intent intent = new Intent(getContext(), CameraPreviewActivity.class);
startActivityForResult(intent, 111);
2) Now CameraPreviewActivity activity will open and it will start camera activity and returns the result to fragment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, FragmentAccPhoto.REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(RESULT_OK, data);
finish();
}

activity does not close after taking picture Android

Hi i have used SurfaceView and taking picture by below code
First i am starting activity by this code
startActivityForResult(new Intent(PictureEditor.this, CustomCamera.class), CAMERA_REQUEST3);
and then getting result from this code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST3) {
BitmapFactory.Options abc = new BitmapFactory.Options();
abc.inJustDecodeBounds = true;
BitmapFactory.decodeFile((Environment.getExternalStorageDirectory() + File.separator + "tester.png"), abc);
abc.inSampleSize = calculateInSampleSize(abc, w, h) + 1;
abc.inJustDecodeBounds = false;
view.setBackBitmap(BitmapFactory.decodeFile((Environment.getExternalStorageDirectory() + File.separator + "tester.png"), abc));
}
Now the CustomeCamera Class's code is below
// / Handles when mTakePicture is clicked
private OnClickListener mTakePictureAction = new OnClickListener() {
#Override
public void onClick(View v) {
if (mCamera != null)
mCamera.takePicture(CustomCamera.this);
}
};
Then
#Override
public void takePicture(Activity activity) {
if (mCamera != null)
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent returnIntent = new Intent();
activity.setResult(mActivity.RESULT_OK, returnIntent);
activity.finish();
}
the problem is image is captured but the activity is not getting finish! Can anybody suggest me what to do!
you need to write code for finish activity in onActivityResult() in previous activity from where this activity starts.
So your previous activity finish . . .
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case YOUR_REQUEST_CODE:
finish();
}
}
Edit :
First change this code.
Intent returnIntent = new Intent();
activity.setResult(Activity.RESULT_OK, returnIntent);
activity.finish();
then in onActivityResult() first check the request code condition then after in request code condition check result code condition.
I have a strong feeling that main (UI) thread is stuck while
#Override
public void takePicture(Activity activity) {
if (mCamera != null)
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent returnIntent = new Intent();
activity.setResult(mActivity.RESULT_OK, returnIntent);
activity.finish();
}
I am not too sure where it is being stuck (from code example above),
possibilities
1) picture cannot be saved
2) picture cannot be encoded
and etc
You can test it by running debugger

camera activity does not return to the previous/caller activity

I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}

Categories

Resources