Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void downloadButtonClicked(Book book) {
- final int id = book.getId();
- final String title = book.getTitle();
- Thread t = new Thread(){
- @Override
- public void run(){
- downloadHandler.sendMessage(download(id, title));
- }
- };
- t.start();
- }
- private Message download(int id, String title) {
- Log.d("downloadASDF", "made it to download");
- URL downloadURL;
- String filePath = getExternalFilesDir(null).toString() +
- getResources().getString(R.string.audioBookDir) + "/" + title + ".wav";
- filePath = filePath.replace(" ", "");
- File outputFile = new File(filePath);
- outputFile.getParentFile().mkdirs();
- try {
- downloadURL = new URL(getResources().getString(R.string.bookDownload)+ id);
- URLConnection connection = downloadURL.openConnection();
- int connectionLength = connection.getContentLength();
- Log.d("downloadASDF", downloadURL.toString());
- Log.d("downloadASDF", "opened connection " + connectionLength);
- DataInputStream inputStream = new DataInputStream(downloadURL.openStream());
- byte[] buffer = new byte[connectionLength];
- inputStream.readFully(buffer);
- inputStream.close();
- Log.d("downloadASDF", "a" + outputFile.getAbsolutePath());
- if (!outputFile.exists())
- outputFile.createNewFile();
- DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(outputFile));
- outputStream.write(buffer);
- outputStream.flush();
- outputStream.close();
- } catch (MalformedURLException ex) {
- ex.printStackTrace();
- Log.d("Malformed URL", "qwert");
- return null;
- } catch (IOException ex) {
- ex.printStackTrace();
- Log.d("IOException", "werty");
- return null;
- }
- Message msg = Message.obtain();
- msg.what = 1;
- return msg;
- }
- Handler downloadHandler = new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(Message msg) {
- Log.d("handlerASDF", "made it to handler");
- if (msg == null) {
- //TODO worry about this later
- }
- else {
- MainActivity.this.bookDetailsFragment.downloaded = true;
- // MainActivity.this.bookDetailsFragment.refreshDownload();
- }
- return true;}
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement