안녕하세요.
안드로이드 앱에서 HTML 파일을 앱(App) 안에 저장하고 저장한 HTML 파일을 Webview와 연동하는 방법을 확인해 보았습니다.
우선 assets에 파일을 넣는 경우,New 해서 만들 수도 있겠지만, 간단하게 자신의 프로젝트 소스 폴더(...\app\src\main\)에 assets라고 만들고 파일들을 넣어두면 스튜디오에 나타납니다.
그리고 앱에서 접근하는 방법은 여러 좋은 사이트들이 있어서 저는 간단한 내용 설명들만 하겠습니다.
<참고 사이트 1> 사이트에 나온 것처럼 파일을
AssetManager assetMgr = this.getAssets();
해서 assets 안에 있는 파일들을 검색할 수 있습니다.
그런데 여기서 조금 주의할 것이
assets = asssetMgr.list("/") 과 assets = asssetMgr.list("")
If you like to list your files in the root-directory of your app, please pass "/" as parameter to the list()-method
앱의 루트의 리스트를 보려면 "/", 없으면 그냥 assets의 경로를 읽어 온다고 합니다. <참고 사이트 2>
그런데 파일리스트를 읽어 오면 알겠지만, 리스트의 내용으로 오지만 폴더 정보를 구분해야 하고 assets 안에 폴더가 있으면 재귀호출 등을 해주어야 합니다.
단지 assets 폴더 안에 생성한 폴더 안에 html을 webview에 나타내고 싶을 뿐인데....
그런데 답은 의외로 간단합니다.
file:///android_asset/myPrivate/index.html 로 지정하면 Webview 링크로 사용가능합니다.
<참고 사이트 3>
이렇게 하면 저장된 html 파일을 webview로 볼 수가 있습니다.
참고로, 작업하면서 assets에 있는 txt 파일 읽어와서 ArraryList에 넣어 두는 방법입니다.
ArrayList listString = new ArrayList<>();
try {
InputStream inputStream = assetMgr.open("list.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line="";
while((line=reader.readLine())!=null) { listString.add(line); }
reader.close();
inputStream.close();
} catch (IOException e) {
//Log.d("TAG", "Exception ERROR" );
e.printStackTrace();
} finally { }
감사합니다.
<참고 사이트>
1. 안드로이드 Asset file 사용하기 (모두 읽어오기)
https://evnt-hrzn.tistory.com/23
2. Working with Assets in Android
https://coderwall.com/p/0ldbgw/working-with-assets-in-android
3. 안드로이드 웹뷰 assets html 파일 사용하기
https://pilot376.tistory.com/70
'Programming > Android' 카테고리의 다른 글
아두이노 나노(Arduino Nano 33 IoT) BLE 값을 송수신 가능한 BLE 안드로이드 앱 만들기 (2) | 2025.01.25 |
---|---|
플래그먼트 갱신, 새로고침 하는 방법 (fragment refresh) (2) | 2024.12.28 |
exposed beyond app through ClipData.Item.getUri() 에러 발생시 (0) | 2024.12.26 |
안드로이드 앱에서 오버레이 화면 사용하는 방법 (0) | 2024.12.23 |
AndroidUSBCamera:libausbc:3.2.8 버전에서 Received status code 401 from server: Unauthorized에러 발생 시 (2) | 2024.12.11 |