Pick .mp3 file from sdcard in preferencescreen
Asked Answered
P

1

0

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

Palanquin answered 6/6, 2016 at 11:7 Comment(0)
F
0

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;
            }
        });
Footgear answered 6/6, 2016 at 11:10 Comment(6)
in startActivityForResult second argument is not integerPalanquin
its character sequncePalanquin
Second argument is unique id for checking response your get, and generally it is int, you can take it as declare first at top int PICK_SONG = 1, and then pass it as second argument, When you will get result in onActivityResult, you need to match this tag for proper response.Footgear
hey is it possible to store selected mp3 file to raw directory?Palanquin
nope you can't do it, instead of that you should store file path in db or preferences and use it, it will reduce memory and run faster.Footgear
how to store file path in PreferencePalanquin

© 2022 - 2024 — McMap. All rights reserved.