간단하게(?) 제가 테스트해 본 코드를 올립니다. File 접근권한 등을 처리해야 하므로 FilePath Provider를 제공해 주어야 하고,
Camera permission도 처리해주어야 합니다.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<application
...>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapplication.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
res/xml/file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
Fragment
public class TakePhotoFragment extends Fragment {
private FragmentTakePhotoBinding binding;
private Uri photoUri;
public TakePhotoFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentTakePhotoBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.takePhotoBtn.setOnClickListener(v ->
requestPermissionOrTakePhoto()
);
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private final ActivityResultLauncher<String> permissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean isGranted) {
photoUri = takePhoto();
}
}
);
private final ActivityResultLauncher<Uri> takeImageResult = registerForActivityResult(
new ActivityResultContracts.TakePicture(), new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean isSuccess) {
if (!isSuccess) return;
loadImage(photoUri);
}
}
);
private Uri createPhotoUri() throws IOException {
return FileProvider.getUriForFile(requireContext(),
"com.example.myapplication.fileprovider",
createImageFile()
);
}
private File createImageFile() throws IOException {
// Create an image file name
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
}
private boolean cameraPermissionGranted() {
return ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED;
}
private void requestCameraPermission() {
permissionResult.launch(Manifest.permission.CAMERA);
}
private void requestPermissionOrTakePhoto() {
if (!cameraPermissionGranted()) {
requestCameraPermission();
return;
}
photoUri = takePhoto();
}
private Uri takePhoto() {
try {
Uri photoUri = createPhotoUri();
takeImageResult.launch(photoUri);
return photoUri;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void loadImage(Uri uri) {
Glide.with(requireView())
.load(uri)
.placeholder(R.drawable.lake)
.into(binding.imageView);
}
}