使用Camera Intent拍照永远不会回到onActivityResult()

时间:2014-01-09 16:27:35

标签: android android-camera onactivityresult

我有一个从TabActivity调用的活动(我知道它已被弃用):

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

这是一个拍摄照片,保存并设置缩略图的活动。它适用于很多设备,但在三星Galaxy Pocket Galaxy Wonder (总是Android 2.x)有时当我启动相机时意图,它永远不会回到onActivityResult(),这意味着:

  1. 我拍了照片
  2. 我保存
  3. 我没有回到onActivityResult()
  4. 它允许我拍摄另一张照片
  5. 并循环......我再也没有回来,如果按finish(),我会回到当前的活动。

    我已经阅读了StackOverflow关于做getParent().startActivityForResult(Intent,RequestCode);之类的内容但是没有用。

    这是我拍照的方式:

    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.
            }
        }
    }
    

    提前致谢!!


    我的代码:

    onCreate上的

    photo是一个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)函数:

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

    我的onActivityResult功能:

    @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();
                }
        }
        }
    
    }
    

    函数sacarfoto()给出了一个String但有时可以用于执行一个进程,但是在这个错误中并不重要。

1 个答案:

答案 0 :(得分:1)

我很确定你认为它永远不会被归还,但事实上它正在返回RESULT_CANCEL。尝试在if(result == RESULT_CANCEL)中添加onActivityResult。也许相机不可能保存图片或类似的东西。