@Override protected Void doInBackground(ArrayList... params) { try { String fileLocation = String.valueOf(params[0].get(0)); String fileName = String.valueOf(params[0].get(1)); String folder = String.valueOf(params[0].get(2)); String path = Constants.ASSETS_IP + fileLocation + fileName; URL u = new URL(path); HttpParams httpParameters = new BasicHttpParams(); HttpURLConnection c = (HttpURLConnection) u.openConnection(); int timeoutConnection = 3000; int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); InputStream is = c.getInputStream(); FileOutputStream fos = new FileOutputStream(new File(folder + "/" + fileName)); int bytesRead = 0; byte[] buffer = new byte[4096]; while ((bytesRead = is.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } fos.close(); is.close(); c.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
public synchronized void writeProgress(int current_file, int total_files) { try { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(resume_file, false))); out.writeInt(current_file); out.writeInt(total_files); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e(TAG, "writeProgress resume.txt not found."); } catch (IOException ex) { Log.e(TAG, "Unable to create resume.txt."); } }
public void mOnClick(View v) { switch (v.getId()) { case R.id.test: String rootdir = Environment.getRootDirectory().getAbsolutePath(); String datadir = Environment.getDataDirectory().getAbsolutePath(); String cachedir = Environment.getDownloadCacheDirectory().getAbsolutePath(); mEdit.setText( String.format( "ext = %s\nroot=%s\ndata=%s\ncache=%s", mSdPath, rootdir, datadir, cachedir)); break; case R.id.save: File dir = new File(mSdPath + "/dir"); dir.mkdir(); File file = new File(mSdPath + "/dir/file.txt"); try { FileOutputStream fos = new FileOutputStream(file); String str = "This file exists in SDcard"; fos.write(str.getBytes()); fos.close(); mEdit.setText("write success"); } catch (FileNotFoundException e) { mEdit.setText("File Not Found." + e.getMessage()); } catch (SecurityException e) { mEdit.setText("Security Exception"); } catch (Exception e) { mEdit.setText(e.getMessage()); } break; case R.id.load: try { FileInputStream fis = new FileInputStream(mSdPath + "/dir/file.txt"); byte[] data = new byte[fis.available()]; while (fis.read(data) != -1) {; } fis.close(); mEdit.setText(new String(data)); } catch (FileNotFoundException e) { mEdit.setText("File Not Found"); } catch (Exception e) {; } break; } }