jupper
jupper

Reputation: 103

onActivityResult is only called after pushing the backbutton

I'm learning programming with android now, and I build an app, to take a picture with the camera with this tutorial.

http://developer.android.com/training/camera/photobasics.html

But onActivity is not called automaticly, it is only called when I push the back button, after taking a picture with my device.

package com.olchowski.valentin.cam2dial;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    String mCurrentPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


    if (intent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Toast.makeText(this, "Exception", Toast.LENGTH_SHORT).show();
        }

        if (photoFile != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
        } else {
            Toast.makeText(this, "Exception", Toast.LENGTH_SHORT).show();
        }
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(this, "RENDERING", Toast.LENGTH_SHORT).show();
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Toast.makeText(this, "PHOTO TAKEN", Toast.LENGTH_SHORT).show();
        finish();
    }
}

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName,".jpg", storageDir);
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
}

Upvotes: 0

Views: 85

Answers (4)

Ruchira Randana
Ruchira Randana

Reputation: 4179

If this is a limitation out of your reach, then the best option is to create your custom activity to capture via a camera. Here is an example on how it can be done. I haven't tested it. But, you should be able to figure it out as it doesn't seem complex.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007359

The behavior of ACTION_IMAGE_CAPTURE is up to the camera app. There are thousands of device models, shipping with hundreds of different camera apps. Plus, there are camera apps downloadable from the Play Store and elsewhere.

Some have bugs.

In this case, ACTION_IMAGE_CAPTURE should return to your app once the user takes a picture. For whatever reason, the implementers of this camera app chose not to do that, or did not test ACTION_IMAGE_CAPTURE well.

There is nothing you can really do about this, other than not use ACTION_IMAGE_CAPTURE (e.g., use the camera APIs directly).

Upvotes: 1

Kuffs
Kuffs

Reputation: 35661

onActivityResult() is called when the Activity you are calling closes so this expected behaviour.

Pressing the "Back" key closes the Activity allowing the result to be delivered.

You cannot get a result from an Activity that is still open.

Upvotes: 0

Babak
Babak

Reputation: 101

I think your problem is that the photos that you take with camera can't save properly on disk because of using createTempFile() method. also you should give some information about what version of android do you use for test and some more details. You are using timestamps for naming your photos taken by camera and by default its guarantee that your file names are unique. so using createTempFile() method is redundant. I try your code with new File() creation method and its worked properly.

Upvotes: 0

Related Questions