【问题标题】:Android ACTION_IMAGE_CAPTURE sometimes not calling onActivityResultAndroid ACTION_IMAGE_CAPTURE 有时不调用 onActivityResult
【发布时间】:2011-12-04 06:40:56
【问题描述】:

在我们的代码中,我们使用如下所示的 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 不会被写入)。我的代码有什么问题?

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

【问题讨论】:

标签: java android camera android-camera


【解决方案1】:

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

【讨论】:

  • 它在显示相机时暂停(如预期的那样),然后调用 WIN DEATH,然后调用我在 Manifest 中指定的 Starter Activity
  • 查看堆栈跟踪并找出导致 WIN DEATH 的代码将很有用。看到这里有一些想法stackoverflow.com/questions/6256730/…
  • 是的,它正在被杀死。现在如何克服呢?
【解决方案2】:
 I faced same problem , once check have you put any tag related to History ?

不要在清单中放置 android:noHistory="true" 标签 如果您在清单内的活动中使用 android:noHistory="true" ,它将 停止后从堆栈中删除。

注意:如果您使用的是选项卡活动,那么您也不应该使用 android:noHistory="true" 或者简单地将 android:noHistory="false" 放在清单内的活动中。

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

【讨论】:

    【解决方案3】:

    根据: 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2018-10-20
      相关资源
      最近更新 更多