0

Problem: When i take foto from camera intent and camera has any orientation than activity, i get null pointer exception from activity in BitmapFactory.decodeFile. (for example activity has vertical orientation, and camera has horizontal) My code:

 private void dispatchTakePictureIntent() throws IOException {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            System.out.println("file1986 - can not");
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

then

 private File createImageFile() throws IOException {
    // Create an image file name
    String imageFileName = "temp_v_" + getIntent().getStringExtra("idVehicle") + "_p_" + getIntent().getStringExtra("idPlace");
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

then

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
            Bitmap img = BitmapFactory.decodeFile(mCurrentPhotoPath,options);
            resizeImage(img,mCurrentPhotoPath);
            foto.setImageBitmap(img);
    }
}



 public static void resizeImage(Bitmap source, String file) {
    Bitmap tempImage = Bitmap.createScaledBitmap(source, source.getWidth() / 5, source.getHeight() / 5, false);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    tempImage.compress(Bitmap.CompressFormat.JPEG, 80, bytes);
    File f = new File(file);
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Logs error:

 11-13 01:16:34.771 31144-31144/mobile.observer.marketirs.com.observermobile E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
11-13 01:16:34.773 31144-31144/mobile.observer.marketirs.com.observermobile E/test: Exception
11-13 01:16:34.783 31144-31144/mobile.observer.marketirs.com.observermobile E/AndroidRuntime: FATAL EXCEPTION: main
11-13 01:16:34.783 31144-31144/mobile.observer.marketirs.com.observermobile E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {mobile.observer.marketirs.com.observermobile/mobile.observer.marketirs.com.observermobile.PhotoActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/temp_v_112_p_12711790753674.jpg typ=image/jpeg (has extras) }} to activity {mobile.observer.marketirs.com.observermobile/mobile.observer.marketirs.com.observermobile.PhotoActivity}: java.lang.NullPointerException
Rooban
  • 248
  • 1
  • 15
Boris Zhguchev
  • 313
  • 4
  • 12

1 Answers1

0

i have solved this task. The problem was the activity would be restarted having changed orientation. And all envs will be null or default value.

this post stackoverflow

Community
  • 1
  • 1
Boris Zhguchev
  • 313
  • 4
  • 12