我試圖用這段代碼檢查一個文件是否存在於我的SD卡上,但我遇到了一些問題。我的Android手機上的API版本是19,應用程序的API版本是19,但是其他應用程序有很多例外,我不想像zedge那樣使用它。請給我一些關於如何檢查該文件是否存在的提示。Android:如何檢查一個文件是否存在於我的SD卡上
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;
}
}
我的清單文件是這樣的:
<?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>
你會發現很多鏈接和相同的問題以及他們的解決方案 – Shekhar
你的問題太含糊。您已經在檢查文件是否存在。 「其他應用程序的例外」是什麼意思? –