Skip to content

Instantly share code, notes, and snippets.

@alitamoor65
Created April 9, 2018 17:00
Show Gist options
  • Save alitamoor65/47315d70f5579fdea05a54eff6c3c087 to your computer and use it in GitHub Desktop.
Save alitamoor65/47315d70f5579fdea05a54eff6c3c087 to your computer and use it in GitHub Desktop.
Copy All Files from Assets to SD Card
private String storeImage(Bitmap bitmap,String fileName){
String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FilterImages";
File dir = new File(dirPath);
if(!dir.exists()){
dir.mkdir();
}
File file = new File(dirPath,fileName);
try{
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
//Toast.makeText(this, "Saved: " + file, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String filePath = dirPath + "/" + fileName;
return filePath;
}
public void copyFileOrDir(String path) {
AssetManager assetManager = this.getAssets();
String assets[] = null;
try {
assets = assetManager.list(path);
if (assets.length == 0) {
copyFile(path);
} else {
String fullPath = "data/data/" + this.getPackageName() + "/" + path;
Log.i("INFO","Copying to : " + fullPath);
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
for (int i = 0; i < assets.length; ++i) {
copyFileOrDir(path + "/" + assets[i]);
}
}
} catch (IOException ex) {
Log.i("INFO", "I/O Exception");
}
}
private void copyFile(String filename) {
AssetManager assetManager = this.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;
Log.i("INFO","File Name: " + filename);
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.i("INFO", "Catch: " + e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment