CopyPastor

Detecting plagiarism made easy.

Score: -1; Reported for: Open both answers

Possible Plagiarism

Reposted on 2015-10-29

Original Post

Original - Posted on 2013-03-09



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I got a solution. If the server supports downloading use the following code.
private final int TIMEOUT_CONNECTION = 5000; //5sec private final int TIMEOUT_SOCKET = 30000; //30sec private final int BUFFER_SIZE = 1024 * 5; // 5MB

try { URL url = new URL("http://....");
//Open a connection to that URL. URLConnection ucon = url.openConnection(); ucon.setReadTimeout(TIMEOUT_CONNECTION); ucon.setConnectTimeout(TIMEOUT_SOCKET);
// Define InputStreams to read from the URLConnection. // uses 5KB download buffer InputStream is = ucon.getInputStream(); BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE); FileOutputStream out = new FileOutputStream(file); byte[] buff = new byte[BUFFER_SIZE];
int len = 0; while ((len = in.read(buff)) != -1) { out.write(buff,0,len); } } catch (IOException ioe) { // Handle the error } finally { if(in != null) { try { in.close(); } catch (Exception e) { // Nothing you can do } } if(out != null) { try { out.flush(); out.close(); } catch (Exception e) { // Nothing you can do } } }
it will help.thanks
Execute all in doInBackground like this:
public class MyAsnyc extends AsyncTask<Void, Void,Void>{ public File file ; InputStream is; @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub try{ File path = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); file = new File(path, "DemoPicture.jpg");
// Make sure the Pictures directory exists. path.mkdirs();
URL url = new URL(BASE_URL); /* Open a connection to that URL. */ URLConnection ucon = url.openConnection();
/* * Define InputStreams to read from the URLConnection. */ is = ucon.getInputStream();
OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); Log.d("MY_TAG>>>", "Picture is Readable..."); os.write(data); Log.d("MY_TAG>>>", "Picture is Saved..."); is.close(); os.close();
} catch (IOException e) { Log.d("ImageManager", "Error: " + e); }
return null; }
protected void onPostExecute() {
try { // Tell the media scanner about the new file so that it is // immediately available to the user.
MediaScannerConnection.scanFile(null, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}

        
Present in both answers; Present only in the new answer; Present only in the old answer;