簡體   English   中英

Android ACTION_IMAGE_CAPTURE有時不會調用onActivityResult

[英]Android ACTION_IMAGE_CAPTURE sometimes not calling onActivityResult

在我們的代碼中,我們使用的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);
            }
        }
    }
}

有時,當我按'Ok'時, onActivityResult不被調用( Came not written)。 我的代碼有什么問題?

編輯: 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}出現在代碼中。

你的活動是否有可能被殺死,這就是onActivityResult未被執行的原因? 當相機Intent返回時,通常會執行onActivityResult后跟onResume。 在onPause和onResume方法中放入一個日志語句,並檢查執行順序。

 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”放入manifest中的活動中。

可能是我的解釋是錯的,但我得到了解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM