简体   繁体   中英

Taking picture with Camera Intent never get back to onActivityResult()

I have an activity which is called from a TabActivity (I know it's deprecated) as:

intent2 = new Intent().setClass(this, sin2.class);
spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent2);
tabHost.addTab(spec);   

which is an activity that takes pictures, save them and set a thumbnail. It works perfectly on a lot of devices but on Samsung Galaxy Pocket or Galaxy Wonder (always Android 2.x) sometimes when I start the Camera Intent, it never gets back to onActivityResult() , that means:

  1. I take a picture
  2. I save it
  3. I doesn't get back to onActivityResult()
  4. It allows me to take another picture

and looping... I never get back, if I press finish() , I come back to my current activity.

I've read on StackOverflow about doing stuff like getParent().startActivityForResult(Intent,RequestCode); but doesn't work.

This is how I take my pictures:

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(photofile);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode==RESULT_OK)
    {
        if (requestCode == 1)
        {
            .............. things I do.
        }
    }
}

Thanks in advance!!


MY CODE:

on onCreate ( photo is a BitMap):

        scatta.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            photo = null; 
            if(foto1.getDrawable()==null)
                foto(1);
            else if(foto2.getDrawable()==null)
                foto(2);
            else if(foto3.getDrawable()==null)
                foto(3);
            else
                Toast.makeText(sinistri2.this, "Cancella una foto per poter scattare un'altra", Toast.LENGTH_LONG).show();
        }
    });

foto(int) function:

@SuppressLint("SimpleDateFormat")
public void hacerfoto(int num){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String n = sdf.format(new Date());
String fotoname = "Immagine-"+ n +".jpg";
File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photostorage2 = new File(photostorage, "im");
photostorage2.mkdirs();
photofile = new File(photostorage2, fotoname);

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(photofile);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num);
}

my onActivityResult function:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    String filename = sacarfoto();

    if (requestCode == 1){

        sacarfoto();


        if(photo != null){
            ruta1 = sacarfoto();
            foto1.setBackgroundColor(Color.parseColor("#00000000"));
            photo = f.resize(photo, filename);
            foto1.setImageBitmap(photo);

            cancellare1.setVisibility(View.VISIBLE);

        }
        else{
            cancellare1.setVisibility(View.GONE);
            foto1.setBackgroundResource(R.drawable.fondoicona);
            Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
        }

        try {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(ruta1);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        } catch (Exception e) {

        }
    }
    if (requestCode == 2){
        sacarfoto();
        if(photo != null){
            ruta2 = sacarfoto();
            photo = f.resize(photo, filename);
            foto2.setImageBitmap(photo);
            foto2.setBackgroundColor(Color.parseColor("#00000000"));
            cancellare2.setVisibility(View.VISIBLE);
        }
        else{
            cancellare2.setVisibility(View.GONE);
            foto2.setBackgroundResource(R.drawable.fondoicona);
            Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
        }

        try{
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(ruta2);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        } catch (Exception e) {

        }
    }
    if (requestCode == 3){
         sacarfoto();

        if(photo != null){
            ruta3 = sacarfoto();
            photo = f.resize(photo, filename);
            foto3.setImageBitmap(photo);
            foto3.setBackgroundColor(Color.parseColor("#00000000"));
            cancellare3.setVisibility(View.VISIBLE);

        }
        else{
            cancellare3.setVisibility(View.GONE);
            foto3.setBackgroundResource(R.drawable.fondoicona);
            Toast.makeText(this, "C'è stato un errore, riprova a scattare la foto", Toast.LENGTH_LONG).show();
        }

        try {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(ruta3);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        } catch (Exception e) {

        }
    }
    if (requestCode == 4) {

        try{
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            photo = f.decodeAndResizeFile(new File(selectedImagePath));


        } catch(NullPointerException ex){
            try {
            photo = (Bitmap) data.getExtras().get("data");
            }
            catch (Exception e){
                photo = BitmapFactory.decodeFile(selectedImagePath);        
            }
        }
            if(photo != null){
                if (foto1.getDrawable()==null){
                    photo = f.resize(photo,selectedImagePath);
                    ruta1 = selectedImagePath;
                    foto1.setImageBitmap(photo);
                    foto1.setBackgroundColor(Color.parseColor("#00000000"));
                    cancellare1.setVisibility(View.VISIBLE);
                }
                else if (foto2.getDrawable()==null){
                    photo = f.resize(photo,selectedImagePath);
                    ruta2 = selectedImagePath;
                    foto2.setImageBitmap(photo);
                    foto2.setBackgroundColor(Color.parseColor("#00000000"));
                    cancellare2.setVisibility(View.VISIBLE);
                }
                else if (foto3.getDrawable()==null){
                    photo = f.resize(photo,selectedImagePath);
                    ruta3 = selectedImagePath;
                    foto3.setImageBitmap(photo);
                    foto3.setBackgroundColor(Color.parseColor("#00000000"));
                    cancellare3.setVisibility(View.VISIBLE);
                }
                else
                    Toast.makeText(sinistri2.this, "Cancella una foto per poter scattare un'altra", Toast.LENGTH_LONG).show();


            }
            else{

                Toast.makeText(this, "Non si è potuto riuscire, riprova scattando una foto o scegliendo una foto dalla gallery.", Toast.LENGTH_LONG).show();
            }
    }
    }

}

the function sacarfoto() gives a String but sometimes works for doing a process, but it doesn't matter in this error.

I'm pretty sure that you think that it's never returned, but in fact it's returning a RESULT_CANCEL . Try to add if(result == RESULT_CANCEL) in your onActivityResult . Maybe it was impossible for the camera to save the picture or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM