检查文件存在于Android上的SD卡 [英] Check if file exists on SD card on Android

查看:128
本文介绍了检查文件存在于Android上的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果一个文本文件存在SD卡上。文件名是mytextfile.txt。下面是code:

I want to check if a text file exists on the SD card. The file name is mytextfile.txt. Below is the code:

FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);

如何检查文件是否存在?

How can I check whether this file exists?

推荐答案

这应该可以解决问题,我已经更换了硬codeD SD卡参照推荐的API调用<一href="http://developer.android.com/reference/android/content/Context.html#getExternalCacheDir%28%29">getExternalCacheDir():

This should do the trick, I've replaced the hard coded SDcard reference to the recommended API call getExternalCacheDir():

File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
  //Do action
}

这篇关于检查文件存在于Android上的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆