简体   繁体   中英

How to attach user current locations (Lat,Lon & address) to each image captured by camera in android

How to add user location as a parameter to the images EXIF data in android?

On tap of any image in the gallery list, the image should load in a new view and show the image captured location details.

And this is the code for image capturing and storing it in the device storage.

private void takeImage(){
        camera.takePicture(null, null, new PictureCallback() {
            private File imageFile;
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    // convert byte array into bitmap
                    Bitmap loadedImage = null;
                    Bitmap rotatedBitmap = null;
                    loadedImage = BitmapFactory.decodeByteArray(data, 0,
                            data.length);
                    Matrix rotateMatrix = new Matrix();
                    rotateMatrix.postRotate(rotation);
                    rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                            loadedImage.getWidth(), loadedImage.getHeight(),
                            rotateMatrix, false);
                    String state = Environment.getExternalStorageState();
                    File folder = null;
                    if (state.contains(Environment.MEDIA_MOUNTED)) {
                        folder = new File(Environment
                                .getExternalStorageDirectory() + "/TestCam");
                    } else {
                        folder = new File(Environment
                                .getExternalStorageDirectory() + "/TestCam");
                    }
                    boolean success = true;
                    if (!folder.exists()) {
                        success = folder.mkdirs();
                    }
                    if (success) {
                        Date date = new Date();
                        imageFile = new File(folder.getAbsolutePath()
                                + File.separator
                                + new Timestamp(date.getTime()).toString()
                                + "Image.jpg");
                        imageFile.createNewFile();
                    } else {
                        Toast.makeText(getBaseContext(), "Image Not saved",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
                    // save image into gallery
                    rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);
                    FileOutputStream fout = new FileOutputStream(imageFile);
                    fout.write(ostream.toByteArray());
                    fout.close();
                    ContentValues values = new ContentValues();

                    values.put(Images.Media.DATE_TAKEN,
                            System.currentTimeMillis());
                    values.put(Images.Media.MIME_TYPE, "image/jpeg");
                    values.put(MediaStore.MediaColumns.DATA,
                            imageFile.getAbsolutePath());

                    MainActivity.this.getContentResolver().insert(
                            Images.Media.EXTERNAL_CONTENT_URI, values);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

There is something called ExifInterface in Android.

https://developer.android.com/reference/android/media/ExifInterface.html

You can read huge number of tags (according to its availability) from files.

ExifInterface exifInterface = new ExifInterface(pathToImageCaptured);
exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

Make sure to check the values of attributes before using them.

The return values are rational numbers. So you will need to convert them to decimal.

Please use https://www.javatpoint.com/fraction-to-decimal on how to convert them.

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