删除pdf文件缓存

时间:2018-01-30 09:31:17

标签: android pdf caching xamarin

我有一个用于打开pdf文件的URL。地址不会更改,但pdf文件可能会更改。当pdf文件发生更改通知时,用户会在同一URL处再次打开该文件,但会打开旧文件。当我删除应用程序缓存并重新加载此文件时,一切都很好。 如何从创建URI或Intent或其他内容中删除此缓存?

        public void OpenPdfByUrl(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            url = url.Replace(" ", "%20");
            var uri = Android.Net.Uri.Parse(url);
            var browserIntent = new Intent(Intent.ActionView, uri);

            try
            {
                context.StartActivity(browserIntent);
            }
            catch (ActivityNotFoundException e)
            {
                Console.WriteLine(e);
                Toast.MakeText(context, "Install pdf reader", ToastLength.Short);
            }

        }

1 个答案:

答案 0 :(得分:0)

static int clearCacheFolder(final File dir, final int numDays) {
    int deletedFiles = 0;
    if (dir!= null && dir.isDirectory()) {
        try {
            for (File child:dir.listFiles()) {

                //first delete subdirectories recursively
                if (child.isDirectory()) {
                    deletedFiles += clearCacheFolder(child, numDays);
                }

                //then delete the files and subdirectories in this dir
                //only empty directories can be deleted, so subdirs have been done first
                if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                    if (child.delete()) {
                        deletedFiles++;
                    }
                }
            }
        }
        catch(Exception e) {
            Log.e(TAG, String.format("Failed to clean the cache, error %s", e.getMessage()));
        }
    }
    return deletedFiles;
}

/ *  *从应用程序缓存中删除早于numDays天数的文件  * 0表示所有文件。  * /

public static void clearCache(final Context context, final int numDays) {
    Log.i(TAG, String.format("Starting cache prune, deleting files older than %d days", numDays));
    int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
    Log.i(TAG, String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
}