Android ACTION_IMAGE_CAPTURE sometimes not calling

2020-02-01 23:32发布

In our code we are using getPhoto method that looks like this:

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:

@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). 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.

3条回答
干净又极端
2楼-- · 2020-02-02 00:05
 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 .

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 .

may be my explanation is wrong but I got solution .

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-02 00:15

Is it possible that your activity is getting killed and that is the reason on onActivityResult is not getting executed? When the camera Intent returns, normally onActivityResult followed by onResume will be executed. Put a log statement in your onPause and onResume methods and check the sequence of execution.

查看更多
该账号已被封号
4楼-- · 2020-02-02 00:25

according to : 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:

 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;
    }
查看更多
登录 后发表回答