简体   繁体   English

Android ACTION_IMAGE_CAPTURE有时不会调用onActivityResult

[英]Android ACTION_IMAGE_CAPTURE sometimes not calling onActivityResult

In our code we are using getPhoto method that looks like this: 在我们的代码中,我们使用的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);
}

And onActivityResult: 和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);
            }
        }
    }
}

Sometimes, when I'm pressing 'Ok', onActivityResult is not called ( Came not wrote). 有时,当我按'Ok'时, onActivityResult不被调用( Came not written)。 What is wrong in my code? 我的代码有什么问题?

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

Is it possible that your activity is getting killed and that is the reason on onActivityResult is not getting executed? 你的活动是否有可能被杀死,这就是onActivityResult未被执行的原因? When the camera Intent returns, normally onActivityResult followed by onResume will be executed. 当相机Intent返回时,通常会执行onActivityResult后跟onResume。 Put a log statement in your onPause and onResume methods and check the sequence of execution. 在onPause和onResume方法中放入一个日志语句,并检查执行顺序。

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

Do not put android:noHistory="true" tag in manifest if you use android:noHistory="true" in your activity inside manifest , it will remove from stack after on stop . 如果您在清单中的活动中使用android:noHistory =“true”,请不要在清单中放置android:noHistory =“true”标记,它会在停止后从堆栈中删除。

Note : if you are using tab activity then also you should not use android:noHistory="true" or else simply put android:noHistory="false" in activity inside manifest . 注意:如果你正在使用tab活动,那么你也不应该使用android:noHistory =“true”,否则只需将android:noHistory =“false”放入manifest中的活动中。

may be my explanation is wrong but I got solution . 可能是我的解释是错的,但我得到了解决方案。

according to : https://groups.google.com/forum/#!topic/android-developers/aihsOU8uAzU 根据: 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. 

So the problem is 所以问题是

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

you can solve the problem by setting captureFile the returned value of this method: 你可以通过设置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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要Android ACTION_IMAGE_CAPTURE内存 - Android ACTION_IMAGE_CAPTURE memory needed ACTION_IMAGE_CAPTURE后Android图库未更新 - Android gallery not updated after ACTION_IMAGE_CAPTURE 返回应用前,Android ACTION_IMAGE_CAPTURE意向崩溃 - Android ACTION_IMAGE_CAPTURE intent crashes before returning to app ACTION_IMAGE_CAPTURE中的图片已旋转 - Image from ACTION_IMAGE_CAPTURE Rotated 使用ACTION_IMAGE_CAPTURE Intent时如何正确错误捕获Android相机活动 - How to properly error-catch Android camera activity when using the ACTION_IMAGE_CAPTURE Intent App Crash:ACTION_IMAGE_CAPTURE问题,Android Studio Java类中带有修饰符 - App Crash :ACTION_IMAGE_CAPTURE issue with modifiers in android studio java class 用 ACTION_IMAGE_CAPTURE 克服错误的好方法 - Beautiful way to come over bug with ACTION_IMAGE_CAPTURE 摄影机意图ACTION_IMAGE_CAPTURE并放入额外数据 - Camera Intent ACTION_IMAGE_CAPTURE and put extra data 为什么使用“曲面”视图的相机图像与我使用Intent(ACTION_IMAGE_CAPTURE)捕获的图像不同 - why the images of camera using Surface view is not same as the image i capture using Intent(ACTION_IMAGE_CAPTURE) Android onActivityResult()在图像捕获时返回NullPointerException - Android onActivityResult() returning NullPointerException on image capture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM