Android(Java)
Android 10 (Q) 이상에서 미디어 파일 저장하기
E.I.T.U
2022. 6. 24. 18:06
private void SaveInDcim(String fileName) {
File cache = new File(getCacheDir(), "output.mp4");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.IS_PENDING, 1);
values.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
values.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
try {
Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
FileDescriptor fd = getContentResolver().openFileDescriptor(uri, "w").getFileDescriptor();
FileOutputStream os = new FileOutputStream(fd);
Path path = Paths.get(cache.getPath());
byte[] bytes = Files.readAllBytes(path);
os.write(bytes);
Toast.makeText(this, "저장 완료", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
else {
try {
File output = new File(Environment.getExternalStorageDirectory() + "/DCIM", fileName);
if (!output.exists()) Log.d("SaveInDcim", "Create Output " + output.createNewFile());
FileOutputStream outputStream = new FileOutputStream(output, true);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
Path path = Paths.get(cache.getPath());
byte[] bytes = Files.readAllBytes(path);
outputStream.write(bytes);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
업무상 동영상 파일을 외부 저장소에 저장할 일이 생겼는데
이전에 비트맵은 저장하는 라이브러리를 따로 제작해뒀지만 파일은 처음이라 찾아서 작성해봤다
흘러들어온 프로그래머들에게 도움이 되길