onActivityResult not called after taking a photo i

2019-07-20 05:20发布

问题:

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://stackoverflow.com/a/12277455/2884981 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.

回答1:

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.



回答2:

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");
    }
}

}