Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用EXIF保存捕获图像_Java_Android_Camera_Metadata_Android Camera Intent - Fatal编程技术网

Java 如何使用EXIF保存捕获图像

Java 如何使用EXIF保存捕获图像,java,android,camera,metadata,android-camera-intent,Java,Android,Camera,Metadata,Android Camera Intent,我正在尝试用android本机摄像头拍摄图像,保存图像很好,但不包含通常的EXIF数据(gps标签、方向…) 我需要做什么来保存EXIF @Override public void onClick(View v) { Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); // Ensure

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

MediaScanner不会像应该的那样解析新图像。这闻起来像是一个特定于设备的bug


有关解决方法,请参阅。

这里是保存图像的功能

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;
}
以下是将包含EXIF数据(位置数据)的图像保存到Gallery的方法:
可能的复制我刚刚发现了一件事,在我复制(手动)DICM/Camera文件夹中的图像后,所有的exif数据都可以显示,但不是在原始图像上。不管我在哪里复制图像,只要复制,一切都很好,奇怪…这意味着,图像没有被MediaScanner解析。看天才,谢谢你,爱你的人。。。lolo。。最佳答案-sendBroadcast(新的Intent(Intent.ACTION\u MEDIA\u MOUNTED,Uri.parse(“file://“+Environment.getExternalStorageDirectory()));嗨,艾哈迈德。欢迎来到堆栈溢出。这是答案吗?我看不出任何解释。请参考这些指南。谢谢。当然@Elletlar先生,对不起,我是新手。我已经编辑了我的答案,为其添加了一些解释,请用您的反馈启发我,谢谢
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();
                        }