如何保存具有EXIF信息的截图

3
我正在尝试使用Android本机相机捕获图像,保存的图像质量很好,但不包含通常的EXIF数据(GPS标签,方向等)。
我需要做什么才能同时保存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;
    }

我刚发现了一些奇怪的事情,当我手动复制DICM/Camera文件夹中的图像时,所有的exif数据都可以显示,但原始图像上却不能显示。 - Cohelad
酷!“重复”是什么意思?您读取文件内容并打开一个新文件,然后将其写入该输出流吗? - Alex Cohn
1
这意味着该图像未被MediaScanner解析。请参见https://dev59.com/8HI95IYBdhLWcg3wwwuY。 - Alex Cohn
1
天才,非常感谢,爱你哥们... 哈哈... 最佳答案 - sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(“file://”+ Environment.getExternalStorageDirectory()))); - Cohelad
显示剩余2条评论
3个回答

1

以下是保存带有EXIF数据(位置数据)的图像到相册的方法:

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
你好Ahmad,欢迎来到Stack Overflow。这是一个答案吗?我完全看不到任何解释。请参考这些准则如何回答。谢谢。 - Elletlar
当然,@Elletlar先生,抱歉我是新手。我已经编辑了我的答案并添加了一些解释,请您给予反馈,谢谢。 - Ahmad Wahaj

0
这是保存图像的函数:
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;
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接