Last active
September 5, 2018 01:40
-
-
Save osama-raddad/abc815cd93d5cc51189c to your computer and use it in GitHub Desktop.
create File From InputStream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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