android-file.exists()对于现有文件返回false(对于不同于pdf的任何东西) [英] android - file.exists() returns false for existing file (for anything different than pdf)
本文介绍了android-file.exists()对于现有文件返回false(对于不同于pdf的任何东西)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
两个文件都出现在sdcard上,但是无论存在什么原因,png文件都会返回false.
Both files are present on the sdcard, but for whatever reason exists() returns false the the png file.
//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()方法的实际作用以及它的作用:
Now, I've look at what's under the hood, what the method file.exists() does actually and this is what it does:
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来结束?
May it be that the method finishes by throwing the exception and returning false?
如果是,
- 我怎么做这项工作
- 还有哪些其他选项可用来检查sdcard上是否存在文件?
谢谢.
推荐答案
1您需要获得设备的许可
1 You need get the permission of device
将此添加到AndroidManifest.xml
Add this to 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;
注意:文件名是sdcard中的路径,而不是根目录中的路径.
例如:您要查找
/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的任何东西)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文