我为此感到疯狂:
Log.d("STATE", Environment.getExternalStorageState());
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "work_data");
Log.d("PATH", f.getAbsolutePath());
if (!f.exists()) {
Log.d("MAKE DIR", f.mkdirs() + "");
}
Run Code Online (Sandbox Code Playgroud)
输出日志如下所示:
STATE mounted
PATH /mnt/sdcard/DCIM/work_data
MAKE DIR false
Run Code Online (Sandbox Code Playgroud)
我确保添加正确的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么它无法创建该文件夹.我也mkdir()一步一步地使用,但结果是一样的.请帮我.我用Google搜索了这么多,并且在这个愚蠢的事情上花了至少两天时间.谢谢你的帮助!!
编辑:
大家好!我添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>了<application>标签.这是我的错!但是谢谢大家的回复.
我有这个RingtonePreference(来自Android Studio的默认SettingsActivity):
pref_notification.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:dependency="notifications_alarm"
android:key="notifications_alarm_ringtone"
android:title="@string/pref_title_ringtone"
android:ringtoneType="notification|all"
android:defaultValue="content://settings/system/notification_sound" />
Run Code Online (Sandbox Code Playgroud)
SettingsActivity.java:
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
return;
}
// Add 'general' preferences.
addPreferencesFromResource(R.xml.pref_general);
// Add 'notifications' preferences, and a corresponding header.
PreferenceCategory fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_notifications);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_notification);
bindPreferenceSummaryToValue(findPreference("notifications_alarm_ringtone"));
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
bindPreferenceSummaryToValue(findPreference("notifications_alarm_ringtone"));
}
}
Run Code Online (Sandbox Code Playgroud)
我想将我的应用程序的自定义铃声从res/raw文件夹添加到列表中.(我不需要它们可用于其他应用程序.)