Android ACTION_IMAGE_CAPTURE有时不会调用onActivityResult

时间:2011-12-04 06:40:57

标签: java android camera android-camera

在我们的代码中,我们使用的getPhoto方法如下所示:

public void getPhoto(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    captureFile = new File(getCaptureFilePath());
    captureUri = Uri.fromFile(captureFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);

    startActivityForResult(intent, CAPTURE_IMAGE);
}

onActivityResult:

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

    Log.w(TAG, "Came");

    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE) {
            try {
                captureFile = new File(getCaptureFilePath());
                captureUri = Uri.fromFile(captureFile);

                Bitmap scaledBitmap = decodeFileAndResize(captureFile);
                saveResizedAndCompressedBitmap(scaledBitmap);

                Bitmap rotatedBitmap = convertToRotatedBitmap(scaledBitmap);
                driverPhoto.setImageBitmap(rotatedBitmap);

                Log.w(TAG, "Before recycle");

                if (rotatedBitmap != scaledBitmap) {
                    scaledBitmap.recycle();
                    scaledBitmap = null;
                    System.gc();
                }

                Log.w(TAG, "After recycle");
            } catch (IOException e) {
                BugSenseHandler.log(TAG, e);
            }
        }
    }
}

有时,当我按'确定'时,onActivityResult不会被调用(Came未写入)。我的代码出了什么问题?

编辑: 12-04 12:43:36.040: INFO/WindowManager(145): WIN DEATH: Window{40839990 com.skalar/com.skalar.activities.RegisterActivity paused=false}在未调用onActivityResult时出现在代码中。

3 个答案:

答案 0 :(得分:4)

你的活动是否有可能被杀死,这就是onActivityResult未被执行的原因?当相机Intent返回时,通常会执行onActivityResult后跟onResume。在onPause和onResume方法中放入一个日志语句,并检查执行顺序。

答案 1 :(得分:3)

 I faced same problem , once check have you put any tag related to History ?

不要在清单中放置android:noHistory =“true”标记   如果您在清单中的活动中使用android:noHistory =“true”,它将会   停止后从堆栈中移除。

注意:如果您正在使用tab活动,那么您也不应该使用android:noHistory =“true” 或者简单地在清单中的活动中放入android:noHistory =“false”。

可能是我的解释错了但我得到了解决方案。

答案 2 :(得分:0)

根据: https://groups.google.com/forum/#!topic/android-developers/aihsOU8uAzU

The method that takes care of the capture only returns correctly 
(using the finnish() method,  I think) if the picture can be saved 
correctly in the file specified by that URI. If there's an exception 
(such as an IO exception) it will be captured and nothing will be 
done, so you'll never know. I believe that 'myTestFile' is a file that 
can only be read/written within your own application and that's why 
image_capture can't write to it. 

所以问题是

captureFile = new File(getCaptureFilePath());
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);

您可以通过将captureFile设置为此方法的返回值来解决问题:

 private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File directory = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "your subdirectory for Picture directory");
        if(!directory.exists() && !directory.mkdir())
            return null;
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                directory      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        return image;
    }