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