I am unable to download a file to a specified path on an Android phone from the server, but nothing happens when running this code.
I have used all the required permissions in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
This is my MainActivity class:
private final String PATH = "/storage/sdcard0/BT/";
public void DownloadFromUrl(String fileName1) { //this is the downloader method
try {
URL url = new URL(f);
File file = new File(fileName1);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.setDoOutput(true);
ucon.connect();
FileOutputStream fos = new FileOutputStream(file);
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
// FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
// Log.d("ImageManager", "download ready in"
// + ((System.currentTimeMillis() - startTime) / 1000)
// + " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}