Skip to content

Commit 3c1d1dc

Browse files
committed
missing
1 parent 5fa1cb0 commit 3c1d1dc

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.zeroone.conceal
2+
3+
import android.graphics.Bitmap
4+
import com.zeroone.conceal.model.Constant.DEFAULT_DIRECTORY
5+
import com.zeroone.conceal.model.Constant.DEFAULT_IMAGE_FOLDER
6+
import com.zeroone.conceal.model.Constant.DEFAULT_PREFIX_FILENAME
7+
import com.zeroone.conceal.model.CryptoFile
8+
import java.io.File
9+
import java.io.FileInputStream
10+
import java.io.FileOutputStream
11+
import java.io.IOException
12+
import java.nio.channels.FileChannel
13+
import java.util.*
14+
15+
/**
16+
* @author : hafiq on 24/03/2017.
17+
*/
18+
19+
internal class FileUtils {
20+
companion object {
21+
@JvmStatic
22+
fun moveFile(file: File?, dir: File?): File? {
23+
if (dir == null || file == null)
24+
return null
25+
26+
if (!dir.exists()) {
27+
if (dir.mkdirs()) {
28+
val newFile = File(dir, file.name)
29+
val outputChannel: FileChannel
30+
val inputChannel: FileChannel
31+
try {
32+
outputChannel = FileOutputStream(newFile).channel
33+
inputChannel = FileInputStream(file).channel
34+
inputChannel.transferTo(0, inputChannel.size(), outputChannel)
35+
inputChannel.close()
36+
37+
inputChannel.close()
38+
outputChannel.close()
39+
40+
file.delete()
41+
} catch (e: Exception) {
42+
return null
43+
}
44+
45+
return newFile
46+
}
47+
}
48+
49+
return file
50+
}
51+
52+
@JvmStatic
53+
fun saveBitmap(imageFile: File, bitmap: Bitmap): Boolean {
54+
55+
var fileCreated = false
56+
var bitmapCompressed: Boolean
57+
var streamClosed = false
58+
59+
if (imageFile.exists())
60+
if (!imageFile.delete())
61+
return false
62+
63+
try {
64+
fileCreated = imageFile.createNewFile()
65+
} catch (e: IOException) {
66+
e.printStackTrace()
67+
}
68+
69+
var out: FileOutputStream? = null
70+
try {
71+
out = FileOutputStream(imageFile)
72+
bitmapCompressed = bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
73+
74+
} catch (e: Exception) {
75+
e.printStackTrace()
76+
bitmapCompressed = false
77+
78+
} finally {
79+
if (out != null) {
80+
try {
81+
out.flush()
82+
out.close()
83+
streamClosed = true
84+
85+
} catch (e: IOException) {
86+
e.printStackTrace()
87+
streamClosed = false
88+
}
89+
90+
}
91+
}
92+
93+
return fileCreated && bitmapCompressed && streamClosed
94+
}
95+
96+
@JvmStatic
97+
fun isFileForImage(file: File?): Boolean {
98+
if (file == null)
99+
return false
100+
101+
val okFileExtensions = arrayOf("jpg", "png", "gif", "jpeg")
102+
103+
for (extension in okFileExtensions) {
104+
if (file.name.toLowerCase().endsWith(extension)) {
105+
return true
106+
}
107+
}
108+
return false
109+
}
110+
111+
@JvmStatic
112+
/***
113+
* get default directory
114+
* @return File
115+
*/
116+
fun getDirectory(mFolderName: String): File? {
117+
val file = File(DEFAULT_DIRECTORY + mFolderName + "/" + DEFAULT_IMAGE_FOLDER)
118+
return if (file.exists()) file else null
119+
120+
}
121+
122+
@JvmStatic
123+
/***
124+
* get default folder
125+
* @return File
126+
*/
127+
fun getImageDirectory(mFolderName: String): File? {
128+
val file = File(DEFAULT_DIRECTORY + mFolderName + "/" + DEFAULT_IMAGE_FOLDER)
129+
if (file.mkdirs())
130+
return file
131+
return if (file.exists()) file else null
132+
133+
}
134+
135+
@JvmStatic
136+
/***
137+
* get List of encrypted file
138+
* @param parentDir - root directory
139+
* @return File
140+
*/
141+
fun getListFiles(parentDir: File?): List<CryptoFile> {
142+
val inFiles = ArrayList<CryptoFile>()
143+
try {
144+
if (parentDir != null) {
145+
val files = parentDir.listFiles()
146+
for (file in files!!) {
147+
if (file.isDirectory) {
148+
inFiles.addAll(getListFiles(file))
149+
} else {
150+
if (file.name.startsWith(DEFAULT_PREFIX_FILENAME)) {
151+
val cryptoFile = CryptoFile()
152+
cryptoFile.fileName = file.name
153+
cryptoFile.path = file.absolutePath
154+
cryptoFile.type = file.parent
155+
inFiles.add(cryptoFile)
156+
}
157+
}
158+
}
159+
}
160+
} catch (e: Exception) {
161+
e.printStackTrace()
162+
}
163+
164+
return inFiles
165+
}
166+
167+
}
168+
}

0 commit comments

Comments
 (0)