0

In my application, the code below enables the user to open the camera and take photo and save to Gallery, the code is working and the photo is saved to the gallery, but the problem is that the photo appears in the gallery rotated and in the end of the gallery with date equal to 1970, please can someone help me to fix these two problems? Thanks in advance. Below is my code: code of capturing photo:

private void dispatchTakePictureIntent() {
    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();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "myapplication.project",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, CAMERA_CAPTURE_TAG);
        }
    }
}

code of creating image file:

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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

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

method of saving photo to gallery:

public final void notifyMediaStoreScanner(final File file) {
    try {
        MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
                file.getAbsolutePath(), file.getName(), null);
        getApplicationContext().sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

OnActivityResult method:

if (requestCode == CAMERA_CAPTURE_TAG && resultCode == Activity.RESULT_OK) {
        //Bundle extras = data.getExtras();
        //imageBitmap = (Bitmap) extras.get("data");
        //imageSelected.setImageBitmap(imageBitmap);


        File f =new File(currentPhotoPath);
       notifyMediaStoreScanner(f);

    }
wisam
  • 57
  • 1
  • 12
  • are you using a Samsung device? – Mithun Sarker Shuvro Mar 17 '19 at 18:24
  • please check this out https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a – Mithun Sarker Shuvro Mar 17 '19 at 18:36
  • yes I am using Samsung real device – wisam Mar 17 '19 at 18:50
  • Thanks for your help, I modified the orientation of the bitmap and then I pass the bitmap instead of the file path in the method so it become: MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), mbitmap, file.getName(), null); and the image stored with correct orientation to the gallery – wisam Mar 17 '19 at 20:04
  • But i still have the problem with the wrong date ! – wisam Mar 17 '19 at 20:04

0 Answers0