在preferencescreen中从sdcard中选择.mp3文件

时间:2016-06-06 11:07:00

标签: android android-sharedpreferences filepicker preferencescreen

我如何从android preferenceScreen菜单中的sdcard中选择mp3文件..

这是我的首选屏幕:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory android:title="@string/app_setting">

            <Preference android:title="@string/ChooseRingtone"
                android:key="sdPref" />

        </PreferenceCategory>
</PreferenceScreen>

这里我的SettingActivity:

public class SettingsActivity extends AppCompatPreferenceActivity{
      @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        if(savedInstanceState == null){
            android.app.Fragment infoFragment = new MyPreferenceFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.fragment_container, infoFragment);
            ft.commit();
        }
      public static class MyPreferenceFragment extends PreferenceFragment {

        private AppSettings settings;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref);

            Preference sdPrefs = findPreference("sdPref");
            sdPrefs.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    return false;
                }
            });
        }
}

我不知道的是如何打开SD卡并从整个SD卡中仅选择.mp3扩展名文件。以及如果可能的话如何将该mp3文件存储到我的资产/原始目录

1 个答案:

答案 0 :(得分:0)

尝试以下代码仅选择mp3文件,

       sdPrefs.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {

                 Intent intent_upload = new Intent();
                 //intent_upload.setType("audio/*"); // For All Audio Files
                 intent_upload.setType("audio/mp3"); // For only MP3 Files
                 intent_upload.setAction(Intent.ACTION_GET_CONTENT);
                 startActivityForResult(intent_upload,1);
                 return false;
            }
        });