【问题标题】:How to save capture image with EXIF如何使用 EXIF 保存捕获的图像
【发布时间】:2013-12-28 21:18:59
【问题描述】:

我正在尝试使用 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;
    }

【问题讨论】:

标签: java android camera metadata android-camera-intent


【解决方案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();
                        }

【讨论】:

  • 嗨艾哈迈德。欢迎来到堆栈溢出。这是一个答案吗?我根本看不到任何解释。请参阅这些指南How to Answer。谢谢。
  • 当然@Elletlar先生,对不起,我是新手。我已经编辑了我的答案以添加一些解释,请告诉我您对此的反馈,谢谢
【解决方案2】:

MediaScanner 未按应有的方式解析新图像。这闻起来像是特定于设备的错误。

请参阅Image, saved to sdcard, doesn't appear in Android's Gallery app 了解解决方法。

【讨论】:

    【解决方案3】:

    这里是保存图片的功能,

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多