简体   繁体   中英

startActivityForResult returning without calling onActivityResult

I am trying to capture a photo and save it in SD card. Image uri is generated and when I invoke startActivityForResult it is returning without calling onActivityResult.

  private Uri dispatchTakePictureIntent() {
      Uri uri = null;
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      // Ensure that there's a camera activity to handle the intent
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
          // Create the File where the photo should go
          File photoFile = null;
          try {
              photoFile = createImageFile();
              Log.d("dispatchTakePictureIntent", "inside try");
          } catch (IOException ex) {
              // Error occurred while creating the File
              Log.d("dispatchTakePictureIntent", "inside catch");

          }
          // Continue only if the File was successfully created
          if (photoFile != null) {

              Log.d("dispatchTakePictureIntent", "inside secondif");
              takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                      Uri.fromFile(photoFile));
              Log.d("dispatchTakePictureIntent","after putExtra");

              startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
              uri = Uri.fromFile(photoFile);
              Log.d("dispatchTakePictureIntent", uri.toString());
              return uri;
          }

      }
      return uri;
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.d("onActivityResult", "outside if");
      ImageView recipeImageView = (ImageView)this.findViewById(R.id.dishImage);
      Log.d("onActivityResult", "outside if");
      Log.d("requestcode", requestCode+"");
      Log.d("REQUEST_IMAGE_CAPTURE", REQUEST_IMAGE_CAPTURE+"");
      Log.d("resultcode", resultCode+"");
      Log.d("REESULT_OK", RESULT_OK+"");
      Log.d("onActivityResult", "inside if");
      if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
          Log.d("onActivityResult", "inside if");
          Bundle extras = data.getExtras();
          Bitmap imageBitmap = (Bitmap) extras.get("data");
          recipeImageView.setImageBitmap(imageBitmap);
      }
      Log.d("onActivityResult", "completed if");
  }


String mCurrentPhotoPath;

  private File createImageFile() throws IOException {
      // Create an image file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      String imageFileName = "JPEG_" + timeStamp + "_";
      File storageDir = Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES);
      Log.d("createImageFile", Environment.DIRECTORY_PICTURES);
      Log.d("createImageFile", storageDir.toString());
      Log.d("createImageFile", imageFileName);
      File image = File.createTempFile(
          imageFileName,  /* prefix */
          ".jpg",         /* suffix */
          this.getCacheDir()     /* directory */
          //storageDir
      );
      Log.d("createImageFile", image.toString());
      // Save a file: path for use with ACTION_VIEW intents
      mCurrentPhotoPath = "file:" + image.getAbsolutePath();
      Log.d("createImageFile", mCurrentPhotoPath);
      return image;
  }

However when I just call for thumbnail image without saving it in SD card, onActivityResult is called and thumbnail image is shown

 private void dispatchTakePictureIntentThumb() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

When I checked log messages.

12-23 23:16:15.589: D/createImageFile(2189): /data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

12-23 23:16:15.599: D/createImageFile(2189): file:/data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

12-23 23:16:15.599: D/dispatchTakePictureIntent(2189): inside try

12-23 23:16:15.599: D/dispatchTakePictureIntent(2189): inside secondif

12-23 23:16:15.599: D/dispatchTakePictureIntent(2189): after putExtra

12-23 23:16:15.609: E/SoundPool(374): error loading /system/media/audio/ui/KeypressInvalid.ogg

12-23 23:16:15.619: W/AudioService(374): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg

12-23 23:16:15.619: W/AudioService(374): onLoadSoundEffects(), Error -1 while loading samples

12-23 23:16:15.629: I/ActivityManager(374): START u0 {act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera (has extras)} from pid 2189

12-23 23:16:15.689: D/gralloc(51): Registering a buffer in the process that created it. This may cause memory ordering problems.

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: E/SurfaceFlinger(51): glCheckFramebufferStatusOES error 1613735025

12-23 23:16:15.689: E/SurfaceFlinger(51): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: E/libEGL(51): called unimplemented OpenGL ES API

12-23 23:16:15.689: W/WindowManager(374): Screenshot failure taking screenshot for (328x546) to layer 21040

12-23 23:16:15.909: D/dispatchTakePictureIntent(2189): file:///data/data/com.cookstory/cache/JPEG_20131223_231615_1148844370.jpg

Going through others posts, I made sure following things are fine.

  1. Enabled write permission.
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> 
  1. I have called startActivityForResult() and not startActivity()
  2. android:launchMode="singleInstance", android:noHistory="true" is not present in manifest

In functionality wise, able to open the camera and take photo, but then when we try to save, it remains in the same state.

I have Encountered this problem. Are you use android:launchMode="singleInstance" in your activity's property?

Here is the function to save the Image in External Directory,

public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
    String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
    String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
    try {
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return filePath;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM