How to get to the previous screen on cancelling taking a picture? - android

I am using an intent to call camera and onresult to get the captured image, but if the image is not taken (on pressing cancel), i want to get back to the screen where i called the camera from
i am using this right now....but is there a better way to do this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!(data == null || resultCode == 0)) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
} else {
Intent intent = new Intent(ImageUploaderActivity.this,
ImageUploaderActivity.class);
startActivity(intent);
finish();
}
}

I am doing something similar in my app and here is my code:
if (resultCode == RESULT_OK) {
if (data != null) {
//When all was ok
}
} else if (resultCode == RESULT_CANCELED) {
//When it was canceled, when I press a back button while in camera app.
} else {
//Some other result
}

Related

Android picker intent returns URI of low quality image (thumbnail) Note 8

I am facing a strange issue and I hope someone can help me out.
I have a app which the user can select a picture from the gallery and them make some editions.... The problem happens on some devices, the returned URI of the picture loads a thumbnail of the picture.
On my Note 8 (with dynamic focus mode, AKA Portrait mode) it always returns the thumbnail of those pictures. I have no idea why.
My code:
private void openGallery(){
Intent pickPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try{
startActivityForResult(pickPhotoIntent, PICK_IMAGE_REQUEST);
}catch (ActivityNotFoundException e){
FirebaseCrash.report(e);
if(mToast != null)
mToast.cancel();
mToast = Toast.makeText(getApplicationContext(),getResources().getText(R.string.no_gallery),Toast.LENGTH_LONG);
mToast.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_PRO){
if(resultCode == RESULT_OK){
AnalyticsManager.UserEnteredIn(FirebaseAnalytics.getInstance(this),AnalyticsManager.PRICE);
billingProcessor.purchase(Home.this,ImageXY.APESCT_PRO);
}
}
else if(requestCode == PICK_IMAGE_REQUEST){
if(resultCode == RESULT_OK){
if(data != null && data.getData() != null){
onImageReceived(data.getData());
}else
Toast.makeText(getApplicationContext(),"Image Loading Error",Toast.LENGTH_SHORT).show();
}
}
if (!billingProcessor.handleActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data);
}
public void onImageReceived(Uri imageUri) {
Reference.image_uri = imageUri;
Log.d("PICKER","RETURNED");
Intent i = new Intent(getApplicationContext(),ImageEditingActivity.class);
startActivity(i);
}
Thanks in advance, I hope someone can help me.

Wait for activity to finish in non-activity class

I have a method in my application which starts a new Activity which starts the Camera Application. The Camera Application returns a result (picture taken or not taken), and I store this result in the Intent-Bundle.
In the original method I want to read this bundle, which does not work because the activity is not finished yet.
How can I wait for the activity to finish before going on in my method?
Method
#Override
public boolean startChallenge(Context context) {
Intent cam = new Intent(context, CameraIntent.class);
cam.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(cam);
Boolean done = cam.getExtras().get("done"); // << this fails obviously
}
Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
finish();
}
I would like to keep the structure of the code because the startChallenge() Method is inherited from a superclass (Challenge) and startChallenge is different from type to type (e.g. there is a challenge where you have to take a picture, another challenge where you have to answer something, and so on). The method gets called in another activity, depending on the type of challenge.
startChallenge should have a reference to the Activity that expects the result back to be able to call startActivityForResult
Method
#Override
public boolean startChallenge(Activity activityB) {// Activity B in which you are expecting this result back.
Intent cam = new Intent(activityB, ActivityA.class);
activityB.startActivityForResult(cam, REQUEST_CODE_OPEN_CAMERA);
}
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
setResult(RESULT_OK, intent);
finish();
}
Another approach would be to use LocalBroadcastManager or Otto
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
BusProvider.getInstance().post(new ImageCapturedEvent(true));
} else if (resultCode == RESULT_CANCELED) {
BusProvider.getInstance().post(new ImageCapturedEvent(false));
}
}
finish();
}

Android continuous Image capture after saving first image (Intent - ACTION_IMAGE_CAPTURE)

Hi I am developing an app which requires to take pics which has to be sent over email. As the camera intent starts camera starts and it allows me to capture an image, when i press SAVE image is displayed in the image view.
Now i need to continue taking images, i.e., after pressing SAVE the camera should start again and be ready to capture pics till i click back button.
Could anyone please help?
For now my code is
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(FileColumns.MEDIA_TYPE_IMAGE);
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(camIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
ImageView img=(ImageView)findViewById(R.id.imageView1);
img.setImageURI(fileUri);
}
else if (resultCode == RESULT_CANCELED)
{
}
}
}
Trigger the same intent again on activity result to start the camera again and handle it here only till when you want to capture the images.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
ImageView img=(ImageView)findViewById(R.id.imageView1);
img.setImageURI(fileUri);
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(FileColumns.MEDIA_TYPE_IMAGE);
camIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(camIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
else if (resultCode == RESULT_CANCELED)
{
}
}
}

Intercepting onActivityResult() for Camera application on Android

In my application, I'm trying to make it so the User can press a button, which will allow them to take a picture using the stock camera application on their phone.
I am following the guide to using an external Camera app to capture images that I can use in my own app from the Android Developers Guide (http://developer.android.com/guide/topics/media/camera.html#intent-receive)
I'm having trouble with the onActivityResult() method, it apparently takes in 3 parameters
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Log.w("borre","Image saved to:\n" + data.getData());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
But at the moment, the data Intent is coming back as null, so calling any methods on the Intent parameter throws a NullPointerException
Here's the code I'm using to call up the Camera application (It's basically the same as the code in the guide)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Has anyone had this problem or knows why this Intent is coming back as null?
your are getting data part null bez you are not setting intent.setDataAndType() when you are starting Acitivty.like
public static final String IMAGE_UNSPECIFIED = "image/*";
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, 3);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
if (requestCode == 2) {
Uri uri=data.getData(); //YOU GET DATA HERE
}
//OR
if (requestCode == 3) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????
imageView.setImageBitmap(photo);
}
}
or in your case getting image path use:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//pic path
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
}

Android Built-in Camera Interface wont return image and crashes on cancel

I'd like to ask for your help concerning Android's built in Camera Interface
When i click capture, the screen captures(meaning it freezes)
But when i click ok, it hangs. It's suppose to go back to the main interface but it doesn't.
When i press cancel, it would force close.
the code is pretty long but this is my listener:
#Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data)
{
super.onActivityResult(requestCode,
resultCode, data);
Double x = null;
Toast.makeText(mContext, x.toString(),Toast.LENGTH_LONG);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
image.setImageURI(uri);
break;
}
}
}
thanks..
This is the intent to call the camera interface.
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCamera, CALL_CAMERA);
`protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
if(requestCode == CALL_CAMERA && resultCode == RESULT_OK)
{
if(imageReturnedIntent.getAction() != null)
{
// display image received on the view
Bundle newBundle = imageReturnedIntent.getExtras();
newBitmap = (Bitmap) newBundle.get("data");
if(newBitmap != null)
{
ImageView imageViewProfilePicture = (ImageView) this.findViewById(R.id.imageViewProfilePicture);
imageViewProfilePicture.setImageBitmap(newBitmap);
imageViewProfilePicture.invalidate();
}
}
}}`
Solved!
The startActivityForResult() sub needs to be within the class. I placed mine inside my onclick which was inside my onCreate sub.
Thanks guys:D

Categories

Resources