简体   繁体   English

FileNotFoundException:/storage/emulated/0/JsonParseTutorialCache:打开失败:ENOENT(没有这样的文件或目录)

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

I'm using GridView to load json data holding images.我正在使用 GridView 加载保存图像的 json 数据。 When I built project it happened some exceptions当我构建项目时发生了一些例外

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$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

I want to download, read and display images on GridView, How to fix those exceptions?我想在 GridView 上下载、读取和显示图像,如何解决这些异常?

In my case, it was:就我而言,它是:

If targeting Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's AndroidManifest file.如果面向 Android 10(API 级别 29)或更高版本,请在应用的AndroidManifest文件中将requestLegacyExternalStorage的值设置为true

<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >

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);
    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);
    File f = new File(cacheDir, filename);
    f.createNewFile();
    return f;
}

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.编辑:当您在 >= Android 6.0 上运行代码时,您必须请求在运行时访问外部存储的权限。 Adding those in AndroidManifest.xml isn't enough.在 AndroidManifest.xml 中添加这些是不够的。 You can use this link on how to do it.您可以使用此链接了解如何操作。

You can use Picasso library for showing images in grid view.您可以使用 Picasso 库在网格视图中显示图像。 it is provide one line code for load image.它为加载图像提供一行代码。 Use Library function for load images--使用库函数加载图像--

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/

You can also save yourself the f.createNewFile and use File.createTempFile(imageFileName, ".jpg", storageDir)你也可以自己保存f.createNewFile并使用File.createTempFile(imageFileName, ".jpg", storageDir)

So your method now becomes所以你的方法现在变成

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory) - Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory) Android/Java:I/System.out:错误:java.io.FileNotFoundException:/storage/emulated/0/hermaps/...:打开失败:ENOENT(没有这样的文件或目录) - Android/Java: I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/...: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory) - java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory) 存储/模拟/ 0 /打开失败:ENOENT(没有这样的文件或目录)? - storage/emulated/0/ open faild:ENOENT (no such file or directory)? FileNotFoundException 打开失败:ENOENT(没有这样的文件或目录) - FileNotFoundException open failed: ENOENT (No such file or directory) DocumentException:/storage/emulated/0/droidText/ciccia.pdf:打开失败:ENOENT(无此类文件或目录) - DocumentException:/storage/emulated/0/droidText/ciccia.pdf: open failed: ENOENT (No such file or directory) 打开失败:ENOENT(没有此类文件或目录)内部存储 - open failed: ENOENT (No such file or directory) internal storage java.io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录) - java.io.FileNotFoundException: open failed: ENOENT (No such file or directory) Android - java.io.FileNotFoundException打开失败:ENOENT(没有这样的文件或目录) - Android - java.io.FileNotFoundException open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:/storage/emulated/0/New file.txt:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM