如何在图库中保存照片

时间:2019-07-14 17:09:20

标签: java android android-camera android-gallery

我想实现在我的应用程序中添加照片的功能。该应用程序必须添加用户在ImageView中拍摄的照片并显示在手机的图库中。

我做了第一部分,但是我没有意识到第二部分。我在电话目录中看到了该文件,但未将其添加到图库中。如果某人很容易,请告诉我更多如何在电话库中创建自己的目录。

public File createFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
);
currentPhotoPath = "file://" + image.getAbsolutePath();
return image;

}

将照片添加到图库的功能(取自Google指南https://developer.android.com/training/camera/photobasics

private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
Log.d("camera", "galleryAddPic: image add");

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == RESULT_OK) {
    if (requestCode == REQUEST_IMAGE_CAPTURE){
      Photo.setImageURI(Uri.parse(currentPhotoPath));
      galleryAddPic();
      Log.d("camera", "onActivityResult: photo added");
    }
    else if (requestCode == REQUEST_CATEGORY) {
        presenter.detailCategory((String) data.getSerializableExtra("category"));
    }
}

0 个答案:

没有答案