Skip to content

Instantly share code, notes, and snippets.

@oianmol
Created July 28, 2017 12:18
Show Gist options
  • Save oianmol/adc66f361e6dafdf9ccf3a8f40d636e9 to your computer and use it in GitHub Desktop.
Save oianmol/adc66f361e6dafdf9ccf3a8f40d636e9 to your computer and use it in GitHub Desktop.
File Utils For Gboard edittext extension keyboard
class FileUtils{
@NotNull
public static File buildImageKeyboardSupportUri(@Nullable String extension) {
final long fileId = System.currentTimeMillis();
String filePath = Environment.getExternalStorageDirectory().toString() +
"/keyboardImage/.shared/iks" + String.valueOf(fileId) +
(android.text.TextUtils.isEmpty(extension) ? "" : ("." + extension));
checkAndCreateDir(filePath);
return new File(filePath);
}
public static boolean checkAndCreateDir(String filePath) {
File tmp = new File(filePath);
File parentFile = tmp.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
return false;
}
return true;
}
public static void copyFile(String fileFromPath, String fileToPath)
throws Exception {
InputStream in = null;
OutputStream out = null;
File f = new File(fileToPath);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
int bufferSize = 8196;
try {
in = new FileInputStream(fileFromPath);
out = new FileOutputStream(fileToPath);
int bytesRead = 0;
byte[] buffer = new byte[bufferSize];
while ((bytesRead = in.read(buffer, 0, bufferSize)) != -1) {
out.write(buffer, 0, bytesRead);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment