osama-raddad
9/9/2015 - 7:39 AM

create File From InputStream

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;
        }