Good day. I have such a problem, there is a method that ties down a file from a dropbox, for a direct public link. But when you call it, the program crashes, it is known that the method starts the robot, such as the folder is created. Here is the method code
// Download dictionary to SD card protected static void downloadDictionary () throws IOException { // Check that SD card is mounted if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ return; } // Making directory for dictionary String downDir = Environment.getExternalStorageDirectory() + "/" + DIR; File donwFile = new File(downDir); donwFile.mkdir(); // Downloading dictionary try { URL url = new URL(FILEURL); HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection(); urlConnect.setRequestMethod("GET"); urlConnect.setDoOutput(true); urlConnect.connect(); FileOutputStream fStream = new FileOutputStream(new File(donwFile, DICTIONARY)); InputStream inputStream = urlConnect.getInputStream(); byte[] buffer = new byte[1024]; int lenght = 0; while ((lenght = inputStream.read(buffer)) > 0) { fStream.write(buffer, 0, lenght); } fStream.close(); // Close write stream } catch (MalformedURLException e) { // MalformedURLException } }
The call takes place in onCreate
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (!DictionaryLoader.getSdFile().exists()){ // Downloading dictionary in background try { DictionaryLoader.downloadDictionary(); } catch (IOException e) { // IOExeption }
But when you start, instead of downloading the file, the program just crashes.