每天下载android中的图像

时间:2014-05-31 19:14:44

标签: android image storage wallpaper

我有一个问题,我需要每天下载一张不同的图片,而且我已经使用过这段代码,但这项工作有一天和其他日子都不行......我不知道,请帮助我,是什么问题

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();
    }
}

}

我在WallpaperService的MyWallService.java中调用此函数,我希望每天上午12:10调用下载并将其保存在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");
                    }
             }
 }

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您需要使用AlarmReceiver设置闹钟这里是一些示例代码

虽然

还需要做其他一些事情
  • 手机重启后,您还需要一个boot_completed接收器来设置闹钟。
  • 您可能需要在首选项中保存上次下载时间,以便准确确定成功。
public class DownloadAlarmReceiver extends BroadcastReceiver {
  private static final int INTERVAL_ALARM = 24*60*60*1000; // 1hr 
  @Override
  public void onReceive(Context context, Intent intent) {
      Intent i = new Intent(context, WallpaperService.class);
      i.setAction(Globals.INTENT_DOWNLOAD_IMAGE);
      context.startService(i);    
  }
    public static void setAlarm(Context act) {
        Intent intent = new Intent(act, DownloadAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(act, Globals.ALARM_REQUEST_CODE, intent, 0);

        AlarmManager alarmManager = (AlarmManager) act.getSystemService(Activity.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+INTERVAL_ALARM, INTERVAL_ALARM, pendingIntent);
    }

    public static void cancelAlarm(Context act) {
        Intent intent = new Intent(act, DownloadAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(act, Globals.ALARM_REQUEST_CODE, intent, 0);

        AlarmManager alarmManager = (AlarmManager) act.getSystemService(Activity.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }
}

在清单中添加它以启用它:

<receiver android:name="com.ex.DownloadAlarmReceiver" />

启动完成:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />