android–file.exists()为现有文件返回false(对于不同于pdf的任何内容)

android–file.exists()为现有文件返回false(对于不同于pdf的任何内容),第1张

概述这两个文件都存在于SD卡上,但无论出于何种原因,exists()都会返回false文件.//Stringpath="/mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png";Stringpath="/mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-12

这两个文件都存在于SD卡上,但无论出于何种原因,exists()都会返回false文件.

//String path = "/mnt/sdcard/AndroID/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png";  String path = "/mnt/sdcard/AndroID/data/com.gemoro.toffer/cache/1551619351/0/foto/-1200240592.pdf";file file2 = new file(path);if (null != file2){    if(file2.exists())    {        LOG.x("file exist");    }    else    {        LOG.x("file does not exist");    }}

现在,我看看底层是什么,file.exists()实际上做了什么,这就是它的作用:

public boolean exists(){    return doAccess(F_OK);}private boolean doAccess(int mode){    try    {        return libcore.os.access(path, mode);    }    catch (ErrnoException errnoException)    {        return false;    }}

可能是通过抛出异常并返回false来完成该方法?

如果是这样的话,

>我该怎么做才能做到这一点
>还有哪些其他选项可以检查sdcard上是否存在文件?

谢谢.

解决方法:

1您需要获得设备的许可

将其添加到AndroIDManifest.xml

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

2获取外部存储目录

file sdDir = Environment.getExternalStorageDirectory();

3最后,检查文件

file file = new file(sdDir + filename /* what you want to load in SD card */);if (!file.canRead()) {    return false;}return true;

注意:filename是sdcard中的路径,而不是root中的路径.

例如:你想找到

/mnt/sdcard/AndroID/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png

然后文件名是

./AndroID/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png

.

总结

以上是内存溢出为你收集整理的android – file.exists()为现有文件返回false(对于不同于pdf的任何内容)全部内容,希望文章能够帮你解决android – file.exists()为现有文件返回false(对于不同于pdf的任何内容)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存