Android ACTION_IMAGE_CAPTURE sometimes does not call onActivityResult

In our code, we use the getPhoto method, which 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 click "Ok", onActivityResult not called ( 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 the code when onActivityResult not called.

+4
source share
3 answers

Is it possible that your activity is killed, and why the onActivityResult cause is not running? When the Intent camera returns, the onActivityResult function will usually execute, followed by onResume. Put the log statement in your onPause and onResume methods and check the execution sequence.

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

Do not put android: noHistory = "true" in the manifest if you use android: noHistory = "true" in your activity inside the manifest, it will be removed from the stack after stopping.

Note: if you use a tab, you should also not use android: noHistory = "true" or just put android: noHistory = "false" in the action inside the manifest.

Maybe my explanation is wrong, but I got a solution.

+2
source

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 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 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 to the return 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; } 
0
source

Source: https://habr.com/ru/post/1384541/


All Articles