Pick .mp3 file from sdcard in preferencescreen

泪湿孤枕 提交于 2019-12-25 07:14:05

问题


How i can pick mp3 file from sdcard in android preferenceScreen menu..

here is my preferenceScreen:

<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>

here my 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;
                }
            });
        }
}

what i dont know is how to open sdcard and choose only .mp3 extension files from whole sdcard. and how to store that mp3 file to my asset/raw directory if it possible


回答1:


Try below code for selecting only mp3 files,

       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;
            }
        });


来源:https://stackoverflow.com/questions/37655841/pick-mp3-file-from-sdcard-in-preferencescreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!