保存到图库的图像旋转且日期错误(1970年)

时间:2019-03-17 16:53:47

标签: android image gallery image-capture

在我的应用程序中,下面的代码使用户可以打开相机并拍照并保存到图库,该代码正在工作并且照片已保存到图库,但问题是照片旋转后出现在图库中在日期等于1970的画廊尽头,请有人帮我解决这两个问题吗?提前致谢。下面是我的代码: 拍摄照片的代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(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) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "myapplication.project",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, CAMERA_CAPTURE_TAG);
        }
    }
}

创建图像文件的代码:

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

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

将照片保存到图库的方法:

public final void notifyMediaStoreScanner(final File file) {
    try {
        MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
                file.getAbsolutePath(), file.getName(), null);
        getApplicationContext().sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

OnActivityResult方法:

if (requestCode == CAMERA_CAPTURE_TAG && resultCode == Activity.RESULT_OK) {
        //Bundle extras = data.getExtras();
        //imageBitmap = (Bitmap) extras.get("data");
        //imageSelected.setImageBitmap(imageBitmap);


        File f =new File(currentPhotoPath);
       notifyMediaStoreScanner(f);

    }

0 个答案:

没有答案