|
23 | 23 | import androidx.camera.view.PreviewView; |
24 | 24 |
|
25 | 25 | import com.google.common.util.concurrent.ListenableFuture; |
| 26 | +import android.content.ContentValues; |
| 27 | +import android.os.Environment; |
| 28 | +import android.provider.MediaStore; |
| 29 | +import android.widget.Toast; |
| 30 | +import androidx.camera.core.ImageCapture; |
| 31 | +import androidx.camera.core.ImageCaptureException; |
| 32 | +import androidx.core.content.ContextCompat; |
26 | 33 |
|
| 34 | +import java.text.SimpleDateFormat; |
| 35 | +import java.util.Locale; |
27 | 36 | import java.io.File; |
28 | 37 | import java.text.SimpleDateFormat; |
29 | 38 | import java.util.Locale; |
@@ -100,35 +109,37 @@ private void startCamera() { |
100 | 109 | } |
101 | 110 | }, ContextCompat.getMainExecutor(this)); |
102 | 111 | } |
103 | | - |
104 | 112 | private void takePhoto() { |
105 | 113 | if (imageCapture == null) { |
106 | 114 | Toast.makeText(this, "Camera not initialized", Toast.LENGTH_SHORT).show(); |
107 | 115 | return; |
108 | 116 | } |
109 | 117 |
|
| 118 | + String filename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US) |
| 119 | + .format(System.currentTimeMillis()) + ".jpg"; |
110 | 120 |
|
111 | | - File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "ChAIss"); |
112 | | - if (!storageDir.exists()) { |
113 | | - storageDir.mkdirs(); |
114 | | - } |
115 | | - |
116 | | - File photoFile = new File(storageDir, |
117 | | - new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(System.currentTimeMillis()) + ".jpg"); |
| 121 | + ContentValues contentValues = new ContentValues(); |
| 122 | + contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename); |
| 123 | + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg"); |
| 124 | + contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/ChAIss"); |
118 | 125 |
|
119 | | - ImageCapture.OutputFileOptions outputFileOptions = |
120 | | - new ImageCapture.OutputFileOptions.Builder(photoFile).build(); |
| 126 | + ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions |
| 127 | + .Builder(getContentResolver(), |
| 128 | + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, |
| 129 | + contentValues) |
| 130 | + .build(); |
121 | 131 |
|
122 | | - imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), |
| 132 | + imageCapture.takePicture(outputFileOptions, |
| 133 | + ContextCompat.getMainExecutor(this), |
123 | 134 | new ImageCapture.OnImageSavedCallback() { |
124 | 135 | @Override |
125 | 136 | public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) { |
126 | | - Toast.makeText(CameraActivity.this, "Photo saved: " + photoFile.getAbsolutePath(), Toast.LENGTH_SHORT).show(); |
| 137 | + Toast.makeText(CameraActivity.this, "Photo saved to gallery!", Toast.LENGTH_SHORT).show(); |
127 | 138 | } |
128 | 139 |
|
129 | 140 | @Override |
130 | 141 | public void onError(@NonNull ImageCaptureException exception) { |
131 | | - Toast.makeText(CameraActivity.this, "Failed to capture photo.", Toast.LENGTH_SHORT).show(); |
| 142 | + Toast.makeText(CameraActivity.this, "Failed to save photo.", Toast.LENGTH_SHORT).show(); |
132 | 143 | } |
133 | 144 | }); |
134 | 145 | } |
|
0 commit comments