【发布时间】:2015-07-21 12:02:17
【问题描述】:
我正在尝试使用此代码检查我的 sdcard 上是否存在文件,但我遇到了一些问题。我的 Android 手机上的 API 版本是 19,应用程序的 API 版本是 19,但是其他应用程序有很多例外,我不想使用 zedge 等。请给我一些关于如何检查该文件是否存在的提示。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/test.txt");
if(myFile.exists()){
Log.d("File", "exists");
}
}
public boolean isExternalStorage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
}
我的 Manifest 文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
【问题讨论】:
-
你会发现很多有相同问题的链接以及他们的解决方案
-
你的问题太模糊了。您已经在检查文件是否存在。 “其他应用程序的异常”是什么意思?