Android 5.1.1(API22)平板Picasso缓存错误求助
It looks like you're hitting a compatibility bug with Picasso's default caching mechanism on Android 5.1.1 (API22) — the key error here is the failed removal of the temp journal file, which triggers repeated retry attempts and eventual image load failure. Other Android versions work fine because this file system handling quirk is specific to older API levels.
Let's break down several actionable solutions to resolve this:
1. Use a Custom Cache Directory (Most Reliable Fix)
Picasso's default internal cache directory can run into file system inconsistencies on API22. Switching to an external cache directory avoids these issues entirely. Here's how to set it up:
First, create a custom Picasso instance with a dedicated external cache:
// Get external cache directory (no runtime permission needed on API22) File externalCacheDir = context.getExternalCacheDir(); if (externalCacheDir != null) { File customPicassoCache = new File(externalCacheDir, "picasso-custom-cache"); // Ensure the directory exists if (!customPicassoCache.exists()) { customPicassoCache.mkdirs(); } // Create a 50MB cache (adjust size as needed) Cache picassoCache = new LruCache(customPicassoCache, 50 * 1024 * 1024); // Build and set the custom Picasso singleton Picasso customPicasso = new Picasso.Builder(context) .memoryCache(new LruCache(context)) .downloader(new OkHttp3Downloader(picassoCache)) .build(); Picasso.setSingletonInstance(customPicasso); }
Then use this custom instance in your RecyclerView Adapter:
Picasso.get() .load(contents.get(position).getLogo()) .memoryPolicy(MemoryPolicy.NO_CACHE) .networkPolicy(NetworkPolicy.NO_CACHE) .error(R.mipmap.ic_launcher) .into(holder.ImageView);
Note: If you don't have OkHttp3 integrated yet, add these dependencies to your build.gradle:
implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.squareup.okhttp3:okhttp:4.11.0' implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
2. Adjust Cache Policies to Reduce File System Operations
Your current code forces a full network load every time with NO_CACHE for both memory and disk. This adds unnecessary file system overhead that can trigger the API22 bug. Try modifying the policy:
- If you only need to skip memory cache (keep disk cache):
Picasso.with(context) .load(contents.get(position).getLogo()) .memoryPolicy(MemoryPolicy.NO_CACHE) .error(R.mipmap.ic_launcher) .into(holder.ImageView);
- If possible, remove both
NO_CACHEflags entirely — letting Picasso handle caching normally will minimize file system operations and avoid the error.
3. Pre-Initialize the Cache Directory and Temp File
API22 might fail to create the required temp journal file automatically. You can pre-create it on app startup to avoid the "file not found" error:
File picassoCacheDir = new File(context.getCacheDir(), "picasso-cache"); if (!picassoCacheDir.exists()) { boolean dirCreated = picassoCacheDir.mkdirs(); if (dirCreated) { // Create an empty journal.tmp file File journalTmp = new File(picassoCacheDir, "journal.tmp"); try { journalTmp.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } }
4. Upgrade to the Latest Picasso Version
Older Picasso versions have known compatibility issues with API22. Upgrading to the latest stable release (2.71828) might fix this bug out of the box. Update your dependency:
implementation 'com.squareup.picasso:picasso:2.71828'
Final Recommendations
Start with Solution 1 (custom external cache) — it's the most robust fix that avoids the root cause entirely. If external storage isn't an option, try adjusting your cache policies (Solution 2) or pre-initializing the cache directory (Solution 3). Upgrading Picasso is always a good idea to pick up general bug fixes.
内容的提问来源于stack exchange,提问作者Hadi Ahmadi

