안드로이드 범위 저장소(Scoped Storage) 사용하기
안드로이드 10부터 Scoped Storage가 도입되었다.
내부저장소 접근하기
외부저장소 공용공간 접근하기
- 미디어 파일
- 기타 파일
public class UIImport extends UICommonTitle {
private static final String TAG = UIImport.class.getSimpleName();
private ListView listView;
private TextView btnLoad;
private LinearLayout baseEmpty;
private LocalDao localDao;
private ListAdapter adapter;
private int selectPos = -1;
private List<File> list = new ArrayList<File>();
private ArrayList<SiteBean> siteList = new ArrayList<SiteBean>();
private ArrayList<HoleBean> holeList = new ArrayList<HoleBean>();
ActivityResultLauncher<Intent> importLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Uri fileUri;
if (result.getData() != null) {
//임포트할 파일 uri 얻기
fileUri = result.getData().getData();
try {
//get mimeType
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(String.valueOf(fileUri));
Log.d(TAG, "extension: " + extension);
/*if (!Objects.equals(extension, "acf")) {
ActivityUtil.showToast(UIImport.this, getString(R.string.inaccessible_extension));
return;
}*/
//파일 내용 읽기
String fileContent = readTextFromUri(fileUri);
Log.d(TAG, "fileContent: " + fileContent);
//에이스 디렉터리에 같은 파일 만들기
String fileName = extractFileName(fileUri);
File dir = new File(getFilesDir().getAbsolutePath() + Constants.FILE_LOAD_PATH);
String destFilePath = getFilesDir().getAbsolutePath() + Constants.FILE_LOAD_PATH + "/" + fileName;
File destFile = new File(destFilePath);
FileUtils.makeFile(dir, destFilePath);
//내용 그대로 옮겨
boolean writeResult = FileUtils.writeFile(destFile, fileContent.getBytes());
Log.d(TAG, "writeResult: " + writeResult);
list.add(destFile);
adapter.notifyDataSetChanged();
baseEmpty.setVisibility(View.GONE);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
ActivityUtil.showToast(UIImport.this, getString(R.string.cannot_find_file));
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContent(R.layout.ui_import);
init();
}
private String readTextFromUri(Uri uri) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\r").append("\n");
}
return stringBuilder.toString();
}
private String extractFileName(Uri uri) {
Cursor cursor = getContentResolver()
.query(uri, null, null, null, null, null);
try {
if (cursor != null) {
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
String displayName = null;
while (cursor.moveToNext()) {
displayName = cursor.getString(index);
Log.i(TAG, "Display Name: " + displayName);
}
return displayName;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
return null;
}
private void init() {
//파일관리자로 임포트할 파일 선택하기
if (osVersionCheck10()) startFileManager();
}
private void startFileManager() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
importLauncher.launch(intent);
}
}
}