FileManager
Class Initialize
การเริ่มต้นใช้งาน จะมี 2 กรณีคือ companion object function และ function ที่จะต้องประกาศ object ของคลาสตามตัวอย่าง
FileManager(private val context : Context)
Variables Companion Object
| Name | Description | Type | Default | Variable Type |
|---|
| DEFAULT_BUFFER_SIZE | ขนาดเริ่มต้นของ buffer สามารถแก้ไขได้ | Int | 1024 * 4 | var |
Method Companion Object Overview
| Name | Description | Return |
|---|
| createImageOrVideoFile() | สร้างไฟล์รูปภาพหรือวีดีโอใน Local storage DCIM ใน device | File |
| writeFileFromJson() | เขียนไฟล์นามสกุล .json จาก json String | File |
| createZipFile() | สร้างไฟล์นามสกุล .zip | File |
| renameFile() | แก้ไขชื่อไฟล์จากไฟล์เดิม | File |
| writeTextFile() | เขียน text ลงไฟล์ | Unit |
| isFileExists() | ตรวจสอบว่าไฟล์นี้มีอยู่ที่ path นั้นหรือไม่ | Boolean |
| readTextFile() | อ่าน text จากไฟล์ | String |
| splitFileNameAndExtension() | แยกชื่อไฟล์ กับ นามสกุล ออกจากกัน | Array< String > |
| deleteFile() | ลบไฟล์ | Unit |
| copyFile() | คัดลอกไฟล์จากที่นึงไปอีกที่นึง | Unit |
| createFileFromInputStream() | สร้างไฟล์ จาก InputStream | Completable |
Method Companion Object Details
fun createAlertBuilder(imageOrVdoName: String = DateUtil.getTimestamp().toString() , ext: String) : File
สร้างไฟล์รูปภาพหรือวีดีโอใน Local storage DCIM ใน device
Parameters
| Name | Description | Type | Default |
|---|
| imageOrVdoName | ชื่อไฟล์ของไฟล์รูปหรือวีดีโอ | String | DateUtil.getTimestamp().toString() |
| ext | นามสกุลของไฟล์รูปหรือวีดีโอ | String | |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ที่เราได้สร้างโดยมีชื่อไฟล์และนามสกุลที่เรากำหนด |
fun writeFileFromJson(json : String , fileName : String , targetDirectory : File) : File
เขียนไฟล์นามสกุล .json จาก json String
Parameters
| Name | Description | Type |
|---|
| json | json String ที่เราต้องการเขียนเข้าไฟล์ | String |
| fileName | ชื่อไฟล์ | String |
| targetDirectory | folder path file ของไฟล์ที่จะให้ไฟล์ .json นี้ไปอยู่ | File |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ .json ที่เราได้สร้างโดยมีที่อยู่อยู่ที่เรากำหนด |
fun writeFileFromJson(json : String , fileName : String , targetPath : String) : File
เขียนไฟล์นามสกุล .json จาก json String
Parameters
| Name | Description | Type |
|---|
| json | json String ที่เราต้องการเขียนเข้าไฟล์ | String |
| fileName | ชื่อไฟล์ | String |
| targetPath | path ของไฟล์ที่จะให้ไฟล์ .json นี้ไปอยู่ | String |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ .json ที่เราได้สร้างโดยมีที่อยู่อยู่ที่เรากำหนด |
fun createZipFile(targetFileZipDirectory : File , zipFileName: String , targetFile : File) : File
สร้างไฟล์นามสกุล .zip
Parameters
| Name | Description | Type |
|---|
| targetFileZipDirectory | folder path file ที่จะให้ไฟล์ .zip ที่จะสร้างนั้นไปอยู่ | File |
| zipFileName | ชื่อไฟล์ | String |
| targetFile | folder file ที่ต้องการ zip | File |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ .zip ที่เราได้สร้างโดยมีที่อยู่อยู่ที่เรากำหนด |
fun renameFile(file : File, newName : String) : File
แก้ไขชื่อไฟล์จากไฟล์เดิม
Parameters
| Name | Description | Type |
|---|
| file | ไฟล์เดิมที่ต้องการเปลี่ยนชื่อ | File |
| newName | ชื่อไฟล์ใหม่ | String |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ที่มีชื่อใหม่ |
fun writeTextFile(folderPath: String, fileName: String?, text: String)
เขียน text ลงไฟล์
Parameters
| Name | Description | Type |
|---|
| folderPath | path ไฟล์ที่ต้องการให้ text ไฟล์อยู่ | String |
| fileName | ชื่อไฟล์ | String? |
| text | ข้อมูล text ที่ต้องการเขียน | String |
fun isFileExists(folderPath: String) : Boolean
ตรวจสอบว่าไฟล์นี้มีอยู่ที่ path นั้นหรือไม่
Parameters
| Name | Description | Type |
|---|
| folderPath | folder file path ที่ต้องการตรวจสอบ | String |
Returns
| Type | Description |
|---|
| Boolean | Return true เมื่อไฟล์นั้นมีอยู่ที่ path นั้น และ Return false เมื่อไฟล์นั้นไม่มีอยู่ที่ path นั้น |
fun readTextFile(fullPathFile: String) : String
อ่าน text จากไฟล์
Parameters
| Name | Description | Type |
|---|
| fullPathFile | folder file path ที่ต้องการจะอ่าน | String |
Returns
| Type | Description |
|---|
| String | Return String text ทั้งหมดที่ได้จากไฟล์ที่เราอ่าน |
fun splitFileNameAndExtension(fileName: String) : Array<String>
แยกชื่อไฟล์ กับ นามสกุล ออกจากกัน
Parameters
| Name | Description | Type |
|---|
| fileName | ชื่อไฟล์ที่มีนามสกุลด้วย เช่น test.txt | String |
Returns
| Type | Description |
|---|
| Array< String > | Return Array ของ ชื่อไฟล์ โดยชื่อไฟล์อยู่ index 0 และนามสกุลไฟล์อยู่ index ที่ 1 เช่น test.txt จะได้ [“test” , “.txt”] |
fun deleteFile(fileOrDirectory : File)
ลบไฟล์
Parameters
| Name | Description | Type |
|---|
| fileOrDirectory | folder file หรือ file ที่ต้องการจะลบ | File |
fun copyFile(input: InputStream, output: OutputStream)
คัดลอกไฟล์จากที่นึงไปอีกที่นึง
Parameters
| Name | Description | Type |
|---|
| input | InputStream ที่ต้องการคัดลอก | InputStream |
| output | OutputStream ที่จะ output ไปอีกที่ | OutputStream |
fun createFileFromInputStream(inputStream: InputStream, file: File) : Completable
สร้างไฟล์ จาก InputStream
Parameters
| Name | Description | Type |
|---|
| inputStream | InputStream ที่ต้องการจะ output | InputStream |
| file | ไฟล์ที่ต้องการจะให้ Inpustream ไป output ที่ไฟล์นี้ | File |
Returns
| Type | Description |
|---|
| Completable | Return Completable เพื่อให้การทำงานต่อจากนี้ |
Variables class
| Name | Description | Type | Default | Variable Type |
|---|
| INTERNAL_STORAGE_ABSOLUTEPATH | path ของ internal storage ใน device | String | context.filesDir.absolutePath | val |
| INTERNAL_STORAGE_PATH | path ของ internal storage ใน device | String | context.filesDir.path | val |
Method class Overview
| Name | Description | Return |
|---|
| createDirectory() | สร้าง Folder file | File |
| getFileName() | เอาชื่อไฟล์จาก Uri | String? |
| getAlbumCacheDirectory() | เอาไฟล์ cache | File |
| getFileFromUri() | สร้างไฟล์จาก Uri | File |
Method Details
fun createDirectory(path : String , isNeedInternalStoragePath : Boolean = false) : File
สร้าง Folder file
Parameters
| Name | Description | Type | Default |
|---|
| path | path ไฟล์ที่ต้องการจะสร้าง | String | |
| isNeedInternalStoragePath | ต้องการใช้ path internal storage ถ้าเป็น true จะใช้ แต่ถ้า false จะไม่ใช้ เช่น createDirectory(“Data/Music”,true) ทำให้ได้ folder อยู่ที่ path “${INTERNAL_STORAGE_PATH}/Data/Music” | Boolean | false |
Returns
| Type | Description |
|---|
| File | Return folder ไฟล์ที่เราสร้างที่ตำแหน่งนั้น |
fun getFileName(uri: Uri) : String?
เอาชื่อไฟล์จาก Uri
Parameters
| Name | Description | Type |
|---|
| uri | Uri ของไฟล์ | Uri |
Returns
| Type | Description |
|---|
| String? | Return String ที่เป็นชื่อไฟล์ |
fun getAlbumCacheDirectory(albumName : String) : File
เอาไฟล์ cache
Parameters
| Name | Description | Type |
|---|
| albumName | ชื่อไฟล์ cache | String |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ cache |
fun getFileFromUri(uri: Uri) : File
สร้างไฟล์จาก Uri
Parameters
| Name | Description | Type |
|---|
| uri | Uri ที่จะสร้างเป็นไฟล์ | Uri |
Returns
| Type | Description |
|---|
| File | Return ไฟล์ที่ได้จาก Uri |