I want to download a picture from internet url and save it in the external storage. Before i've tried to create a folder called MyApp, but seems to be failing because the logs aren't appearing at the console. This is my code:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/MyApp/";
File fileFolder = new File(path);
try {
// Make sure the Pictures directory exists.
if (!fileFolder.exists()) {
if (!fileFolder.mkdirs()) {
Log.i("myApp", "failed to create directory");
}else{
Log.i("myApp", "the dir has been created");
}
}
newFoto = new File(path, urls[1]);
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
is = connection.getInputStream();
OutputStream os = new FileOutputStream(newFoto);
byte[] buffer = new byte[is.available()];
int bytesRead = 0;
while ((bytesRead = is.read(buffer, 0, buffer.length)) >= 0) {
os.write(buffer, 0, bytesRead);
}
Log.i("myApp", "writing at"+newFoto.getPath());
is.close();
os.close();
return true;
} catch (IOException e) {
Log.i("myApp", "Download has failed: " + e);
return false;
}