【问题标题】:Remove pdf file cache [duplicate]删除 pdf 文件缓存 [重复]
【发布时间】:2018-01-30 09:31:17
【问题描述】:

我有一个用来打开 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);
            }

        }

【问题讨论】:

  • 你解决了吗。我现在也有同样的问题

标签: android pdf caching xamarin


【解决方案1】:
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));
}

【讨论】:

  • 我应该在那里发送什么上下文。应用程序的上下文?
  • 我调用 ClearCache(Application.Context,0),但它总是只找到一个空文件夹并且不删除任何内容,在上传文件之前和上传之后什么都没有添加。也许我发送了错误的上下文
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 2020-09-10
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多