I have a problem, i need download an different image every day, and I've used this code but this work one day and other day dont work... i dont know, help me please, what is the problem.
public class DownloadImageToSdcard {
private String URL_PHOTO = "/{mysite}.com/myimage/";
@SuppressLint("SdCardPath")
private String DIR_FOLDER = "/sdcard/myphotos/";
Calendar calendar = Calendar.getInstance();
public void MakeFolderToPhoto(String NAME_PHOTO) throws FileNotFoundException {
File myDir = new File(DIR_FOLDER);
if(!myDir.exists())
{
myDir.mkdirs();
}
try{
URL url = new URL(URL_PHOTO+NAME_PHOTO);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = NAME_PHOTO;
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
InputStream inputStream = null;
HttpURLConnection httpConn = (HttpURLConnection)ucon;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
FileOutputStream fos = new FileOutputStream(file);
int size = 1024*1024;
byte[] buf = new byte[size];
int byteRead;
while (((byteRead = inputStream.read(buf)) != -1)) {
fos.write(buf, 0, byteRead);
}
fos.close();
}catch(IOException io)
{
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
and i call this function in my MyWallService.java of WallpaperService, i want call to download every day 12:10am and save this in Sdcard
void UpdateWall(int hour, int minut) {
if(hour == 0)
{
if(minut == 10)
{
DownloadImage.MakeFolderToPhoto(NAME_IMAGE_PREF+Integer.toString(calendar.get(Calendar.DAY_OF_MONTH))+".jpg");
}
}
}