3

i'm trying to capture image with android native camera, the save image is good but doesnt contain the usual EXIF data (gps tags, orientation...)

what do i need to do to save also the EXIF?

         @Override
        public void onClick(View v) {

            Intent takePictureIntent = new Intent(android.provider.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) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));

                    imageuri = Uri.fromFile(photoFile);
                    startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST);
                }
            }

            /*Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);*/
        }
    }

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

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }
Cohelad
  • 121
  • 9
  • possible duplicate of [Images taken with ACTION\_IMAGE\_CAPTURE always returns 1 for ExifInterface.TAG\_ORIENTATION on some newer devices](http://stackoverflow.com/questions/8450539/images-taken-with-action-image-capture-always-returns-1-for-exifinterface-tag-or) – Alex Cohn Dec 29 '13 at 13:43
  • I just found out somthing wierd, after i duplicate (manualy) the image inside DICM/Camera folder, all the the exif data can be shown, but not on the original one – Cohelad Dec 29 '13 at 15:47
  • it doesn't matter where i duplicate the image, just duplicate and everything is fine, weird... – Cohelad Dec 29 '13 at 15:55
  • cool! What do you mean by "duplicate"? You read the file contents and open a new file and write to that output stream? – Alex Cohn Dec 29 '13 at 16:03
  • i can take out programmatically the EXIF data also from the original one, the 'bug' that i have is on android gallery, when i see a photo, when press on option menu and then on detail i get all the details on the photo (gps, zoom. flash...) when i press this button on the original one, i don't get the details option, just on the duplicate or even changed name one... – Cohelad Dec 30 '13 at 16:36
  • 1
    Which means, the image is not parsed by the MediaScanner. See http://stackoverflow.com/questions/2170214/image-saved-to-sdcard-doesnt-appear-in-androids-gallery-app – Alex Cohn Dec 30 '13 at 18:50
  • 1
    Genius, thanks alot, love u man... lolo.. the best answer - sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); – Cohelad Dec 30 '13 at 20:11

3 Answers3

1

Following is the method to Save an Image with EXIF Data (Location Data) to Gallery:

private String saveToGallery (Bitmap bitmapImage){
                            ContextWrapper cw = new ContextWrapper(getApplicationContext());
                            // path to Directory
                            String photoDir = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/";
                            File directory = new File(photoDir);

                            // Creates image file with the name "newimage.jpg"
                            File myfilepath = new File(directory, "newimage.jpg");

                            FileOutputStream fos = null;
                            try {
                                fos = new FileOutputStream(myfilepath);
                                // Use the compress method on the BitMap object to write image to the OutputStream
                                bitgallery.compress(Bitmap.CompressFormat.JPEG, 80, fos);

                                fos.flush();
                                fos.close();
                                myfilepath.setReadable(true, false);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            Uri bitmapUri = Uri.fromFile(myfilepath);

                            String currentImageFile = bitmapUri.getPath();

                            //Writes Exif Information to the Image
                            try {
                                ExifInterfaceEx exif = new ExifInterfaceEx(currentImageFile);
                                Log.w("Location", String.valueOf(targetLocation));
                                exif.setLocation(targetLocation);
                                exif.saveAttributes();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            // Updating Gallery with the Image (Sending Broadcast to Gallery)
                            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                            File f = new File(currentImageFile);
                            Uri contentUri = Uri.fromFile(f);
                            mediaScanIntent.setData(contentUri);
                            this.sendBroadcast(mediaScanIntent);
                            return directory.getAbsolutePath();
                        }
  • 1
    Hi Ahmad. Welcome to Stack Overflow. Is this an answer? I cannot see any explanation at all. Please refer to these guidelines [How to Answer](https://stackoverflow.com/help/how-to-answer). Thanks. – Elletlar Jul 27 '18 at 13:35
  • Sure @Elletlar Sir, Sorry I am new to this. I have edited my answer already to add some explanation to it , please enlighten me with your feedback on that, Thanks – Ahmad Wahaj Jul 30 '18 at 07:27
0

The new image is not parsed, as it should be, by the MediaScanner. This smells like a device-specific bug.

See Image, saved to sdcard, doesn't appear in Android's Gallery app for workarounds.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

Here is the function to save the Image,

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;
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58