我正在使用 GridView 加载包含图像的 json 数据。当我构建项目时发生了一些异常
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
文件缓存类:
public class FileCache {
private File cacheDir;
public FileCache(Context context) {
// Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"JsonParseTutorialCache");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) throws IOException {
String filename = String.valueOf(url.hashCode());
// String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
f.createNewFile();
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
日志:
W/System.err: java.io.IOException: open failed: ENOENT (No such file or directory)
W/System.err: at java.io.File.createNewFile(File.java:939)
W/System.err: at com.totoroads.android.app.FileCache.getFile(FileCache.java:33)
W/System.err: at com.totoroads.android.app.ImageLoader.getBitmap(ImageLoader.java:64)
W/System.err: at com.totoroads.android.app.ImageLoader.access$000(ImageLoader.java:29)
W/System.err: at com.totoroads.android.app.ImageLoader$PhotosLoader.run(ImageLoader.java:155)
W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at java.io.File.createNewFile(File.java:932)
W/System.err: ... 9 more
我想在 GridView 上下载、读取和显示图片,如何解决这些异常?
最佳答案
在我的例子中,它是:
如果以 Android 10(API 级别 29)或更高版本为目标,请在应用的 AndroidManifest
文件中将 requestLegacyExternalStorage
的值设置为 true
。
<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >
关于java - 文件未找到异常 :/storage/emulated/0/JsonParseTutorialCache: open failed: ENOENT (No such file or directory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40260445/