java FileNotFoundException:/storage/emulated/0/JsonParseTutorialCache:打开失败:ENOENT(没有那个文件或目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40260445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:04:50  来源:igfitidea点击:

FileNotFoundException: /storage/emulated/0/JsonParseTutorialCache: open failed: ENOENT (No such file or directory)

javaandroidgridview

提问by luongkhanh

I'm using GridView to load json data holding images. When I built project it happened some exceptions

我正在使用 GridView 加载保存图像的 json 数据。当我构建项目时发生了一些例外

AndroidManifest.xml:

AndroidManifest.xml:

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

FileCache class:

文件缓存类:

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

}

}

Logcat:

日志猫:

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
public File getFile(String url) {
    String filename = String.valueOf(url.hashCode());
    // String filename = URLEncoder.encode(url);
    File f = new File(cacheDir, filename);
    return f;
}
0(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

I want to download, read and display images on GridView, How to fix those exceptions?

我想在 GridView 上下载、读取和显示图像,如何解决这些异常?

采纳答案by Srikanth R

The code in FileCache.java is

FileCache.java 中的代码是

public File getFile(String url) {
    String filename = String.valueOf(url.hashCode());
    // String filename = URLEncoder.encode(url);
    File f = new File(cacheDir, filename);
    f.createNewFile();
    return f;
}

A new file object has been created, but the file may not exist. So, replace the above code with

已创建新文件对象,但该文件可能不存在。因此,将上面的代码替换为

public File getFile(String url) {
    String filename = String.valueOf(url.hashCode());
    // String filename = URLEncoder.encode(url);

    return File.createTempFile(filename, ".jpg", cacheDir);
}

Hope, it helps.

希望能帮助到你。

Edit : As you're running your code on >= Android 6.0, you gotta request for permission to access External Storage at runtime. Adding those in AndroidManifest.xml isn't enough. You can use this linkon how to do it.

编辑:当您在 >= Android 6.0 上运行代码时,您必须请求在运行时访问外部存储的权限。在 AndroidManifest.xml 中添加这些是不够的。您可以使用此链接了解如何操作。

回答by Nishchal

You can use Picasso library for showing images in grid view. it is provide one line code for load image. Use Library function for load images--

您可以使用 Picasso 库在网格视图中显示图像。它为加载图像提供一行代码。使用库函数加载图像--

Picasso.with(context).load("YOURIMAGE").into(imageView);

Picasso.with(context) .load(" YOURIMAGE").into(imageView);

You can also resize images according to your requirement. and it is also manage internally cache mechanism

您还可以根据需要调整图像大小。并且它也是管理内部缓存机制

Refer Link for more info - http://square.github.io/picasso/

有关更多信息,请参阅链接 - http://square.github.io/picasso/

回答by Enoobong

You can also save yourself the f.createNewFileand use File.createTempFile(imageFileName, ".jpg", storageDir)

您还可以保存自己f.createNewFile并使用File.createTempFile(imageFileName, ".jpg", storageDir)

So your method now becomes

所以你的方法现在变成

##代码##