相关疑难解决方法(0)

Android:外部存储上的mkdirs()/ mkdir()返回false

我为此感到疯狂:

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>标签.这是我的错!但是谢谢大家的回复.

android android-file android-external-storage

32
推荐指数
7
解决办法
3万
查看次数

如何将我的应用程序在res/raw文件夹中的自定义铃声添加到铃声首选项中

我有这个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文件夹添加到列表中.(我不需要它们可用于其他应用程序.)

android android-custom-view ringtone android-preferences

5
推荐指数
2
解决办法
2932
查看次数