1. 목표 필요한 File을 생성하고 해당 File의 경로(Path)를 확인한다.
2. 개발 환경
- Android Studio 1.4.1(64bit)
- LG G pro (LolliPop)
3. App Coding 과정
1) AndroidManifest.xml 의 수정
: 해당 App.이 File을 read/write할 권한을 주기 위해 <uses-permission ...>을 다음과 같이 준다. 이 과정을 수행하지 않을 경우, compile 및 실행은 문제 없이 되나 실제 File을 생성되지 않는다.
package="com.example.zip.filepathexample" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...
2) .xml file에 파일 생성 여부를 보여 주기 위한 Layout 설정
: 생성된 File의 경로를 보여주기 위해 TextView를 사용하기로 했다. 이 부분은 추후에 List 등을 이용하여 icon화 시키는 등 응용하는 방법을 배워야 겠다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/pathview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Path01"/>
<TextView
android:id="@+id/pathview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Path02"/>
<TextView
android:id="@+id/pathview03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Path03"/>
</LinearLayout>
3) .java file을 통한 Activity 설정
a. external storage에 file directory 생성
: File 생성자를 통해 다음과 같이 file을 저장할 directory를 생성한다.
if (!storeDir.exists()) {
if (!storeDir.mkdirs()) {
Log.d("MyDocApp", "failed to create directory");
return;
}
}
b. file 생성
: 실제 생성할 File을 다음과 같이 생성한다. file name은 해당 명령이 수행되는 시간을 따와서 쓸 수 있도록 SimpleDateFormat().format(new Date());를 사용한다.
temp = new File(storeDir.getPath() + File.separator + "test_"+ timeStamp + ".docx");
if(temp == null){
Log.d(TAG, "Error at creating .docx file, check storage permissions :");
return;
}
c. file 생성 경로의 확인
: 생성된 파일의 경로를 String으로 찍어 봄으로서 파일의 생성 여부를 확인한다. 이 때, 'file 생성자 변수명'.getPath(); 혹은 .getAbsolutePath();, .getCanonicalPath(); 등 file 내 경로 확인 함수가 여러 개가 있었는데 string 값에는 차이가 없었으나, 실행 장치나 compile 방식에 따라 차이가 있을 수도 있다(참고 : http://egloos.zum.com/entireboy/v/4196432).
pathView02.setText(temp.getPath());
try {
pathView03.setText(temp.getCanonicalPath());
} catch (IOException e) {
Log.d(TAG, "Error at creating .docx file, check storage permissions :");
return;
}
4. App 생성 결과
아래 사진과 같이 file path를 확인했는데 android 내에서는 getPath()나 getAbsolutePath(), getCanonicalPath();들의 차이가 확인되지 않았다. 다른 사람들을 블로그를 보면 다소 차이가 있었는데, 아무래도 나는 file을 생성할 때 경로를 모두 찍어 입력해서 그렇지 않은가 싶다.
'개발일지' 카테고리의 다른 글
[Android Camera 활용] 직접 Custom Camera App 개발하기 02 - Intent 활용 + ImageView 추가 (0) | 2015.11.28 |
---|---|
[Android Camera 활용] 직접 Custom Camera App 개발하기 01 - Intent 활용 (6) | 2015.11.27 |
jar 명령어 요약 정리 (0) | 2015.11.27 |
[Apache POI 활용] xmlbeans-2.x.x.jar의 repack을 통한 duplicate class issue 해결 (3) | 2015.11.26 |
Activity 추가 - Intent, component의 활용 ver 1.1(글 옮김) (0) | 2015.11.25 |