onActivityResult not called after taking a photo in Android
Asked Answered
V

3

4

I am using this code but my onActivityResult never gets called. I used to make the request without passing the extra intent to save the image to an SD card and that worked fine - onActivityResult() would get called as I expect it to. But since I added the SD card code - no luck!

Have I added (or missed) something ? I was following https://mcmap.net/q/358105/-android-saved-images-are-low-quality as an example.

Here is my code,

    static final int CAPTURE_IMAGE_CALLBACK = 1;

    private void dispatchTakePictureIntent() 
    {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File photo = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
        {
            photo = new File(android.os.Environment.getExternalStorageDirectory(), "myapp/images/" + File.separator + timeStamp + ".png");
        } 
        else 
        {   
            photo = new File(getCacheDir(), "myapp/images/" + File.separator + timeStamp + ".png");
        }  
        if ( photo != null)
        {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        }

        if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
        {
            startActivityForResult(takePictureIntent, CAPTURE_IMAGE_CALLBACK);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // This never gets hit!
        if (requestCode == CAPTURE_IMAGE_CALLBACK) 
        {
              // etc

I just thought I'd add, inside the camera app once I click the tick - nothing happens. I click the tick repeatedly, it just stays on the camera screen. Control never gets returned to the main app.

Venose answered 29/1, 2014 at 3:24 Comment(1)
I have answered this question before take a look: https://mcmap.net/q/358106/-android-action_image_capture-sometimes-not-calling-onactivityresultIncrease
V
7

I needed to add this:

    photo.getParentFile().mkdirs();
    photo.createNewFile();

I believe the reason it was failing was because the file I was trying to write the image to didn't exist.

Venose answered 29/1, 2014 at 5:24 Comment(2)
bro where these 2 lines code should put? inside onActivityResult?Tomikotomkiel
Inside dispatchTakePictureIntent(), I added it in the if (photo != null) area.Venose
L
1

I use something like this and it works fine:

public class MainActivity extends Activity implements OnClickListener {

Button btnTackPic;
Bitmap bitMap;
static int TAKE_PICTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Setup camera ready for picture clicking

    // add onclick listener to the button
    btnTackPic.setOnClickListener(this);

}

// Take pic
@Override
public void onClick(View view) {

    // create intent with ACTION_IMAGE_CAPTURE action 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // start camera activity
    startActivityForResult(intent, TAKE_PICTURE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
        // get bundle
        Bundle extras = intent.getExtras();

        // get bitmap
        bitMap = (Bitmap) extras.get("data");
    }
}

}

Lovell answered 29/1, 2014 at 3:40 Comment(4)
Hi. Yeah I can do the same thing, it's only when I add the SD Card code that I ran into trouble. And I need the SD Card code to be working as well as reading the thumbnail data as you are.Venose
Shouldn't your SD card code be done after you have taken the image ? ie move that code into the onActivityResult method as you are then assured that you have a photo to save.Lovell
No, you need to pass into the intent the file that you want to save the SD card into. Then I believe a high resolution copy of the photo will be saved into that file. If you try and save to the SD card after you've returned the image to onActivityResult you'd be left with the low resolution bitmap/thumbnail. (I believe this is the case anyway, I can confirm once I get this working!!)Venose
Ah ok seems like you've done a bit of testing yourself. Good luckLovell
M
0

In my case, the onDestroy called instead. Because android system will try to release memory (if our apps in background, and we open camera apps), and the reason it never called onActivityResult method also due to this please make sure you were not using noHistory=true or intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)

e.g

// AbcActivity.kt
val intent = Intent(AbcActivity.this, RegistrationForm.class)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent)

// RegistrationForm.kt
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// do take pictures, and confirm the photo, never going back to the RegistrationForm, instead back to AbcActivity

by removing flags and noHistory=true (in AndroidManifest.xml) my activity recreated and onActivityResult called again, but we need to save the previous state using ViewModel or (onSaveInstance & onRestoreInstance)

Memorandum answered 30/5, 2020 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.