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 = > 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.

  • About Debug, LogCat heard? Does the application have Permission Internet? - ReinRaus
  • Permission is. My emulator starts for a very long time, so I immediately test the program on my smartphone. And I do not see the logs. - bboybboy
  • one
    LogCat and from the smartphone logs shows. - afiki
  • And if you enable usb debugging, you can line by line and on the device to perform. - ReinRaus

2 answers 2

Well, at least google something before asking a question ....

although generally do not go far for example

    Most likely the error is that you work with the network in the main thread (UI thread). Take these actions to AsyncTask, or to a new Thread.