Skip to content

Instantly share code, notes, and snippets.

@osama-raddad
Last active September 5, 2018 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osama-raddad/abc815cd93d5cc51189c to your computer and use it in GitHub Desktop.
Save osama-raddad/abc815cd93d5cc51189c to your computer and use it in GitHub Desktop.
create File From InputStream
private File createFileFromInputStream(InputStream inputStream,String fileName) {
String path = "";
File file = new File(Environment.getExternalStorageDirectory(),
"Files/");
if (!file.exists()) {
if (!file.mkdirs())
Log.d("Files", "Folder not created");
else
Log.d("Files", "Folder created");
} else
Log.d("Files", "Folder present");
path = file.getAbsolutePath();
try {
File f = new File(path+"/"+fileName);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
return f;
} catch (IOException e) {
// Logging exception
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment