selecting ringtone from sd card android

In my android app I added code for selecting the notification tone from built in tones .But I need to add one more option that to select songs from sd cards also. Also better to add conditions for selcting music files only. Please help me.I am giving the codes in my app

public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
        this.startActivityForResult(intent, 5);
        return true;
    }
@Override
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
     {
         if (resultCode == Activity.RESULT_OK && requestCode == 5)
         {
              Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

              if (uri != null)
              {
                  this.chosenRingtone = uri.toString();
              }
              else
              {
                  this.chosenRingtone = null;
              }
                  DatabaseHandler1 dbtone = new DatabaseHandler1(getApplicationContext());
                    int count = dbtone.getRowCount_5();
                    if(count > 0){
                    dbtone.resetTables_5();
                    }
                 dbtone.addUser_5(chosenRingtone);
          }            
      }

I am setting the ringtone here

DatabaseHandler1 db2 = new DatabaseHandler1(context);
        int count = db2.getRowCount_5();
            if(count > 0){
         x =db2.getUserDetails_5();
        String temp =  x.get("tone");
                try{
                    notification.sound = Uri.parse(temp);
                    }
                catch (Exception e) {
                    notification.defaults |= Notification.DEFAULT_SOUND;

                    }
            }
            else{
        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;
            }

Answers


This will give you all media files

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        //your projection statement 
        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM_ID
        };
        //query 
        cursor = this.managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                null,
                null);


        while(cursor.moveToNext()){
                songs.add(cursor.getString(0));
                songs.add(cursor.getString(1));
                songs.add(cursor.getString(2));
                songs.add(cursor.getString(3));
                songs.add(cursor.getString(4));
                songs.add(cursor.getString(5));
                album_id.add((long) cursor.getFloat(6));
        } 
        int a[]= new int[]{R.id.textView1 ,R.id.textView3};//, R.id.textview2};
        ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.items, cursor, new String[]{MediaStore.Audio.Media.TITLE,           MediaStore.Audio.Media.ARTIST/*, MediaStore.Audio.Media.DURATION*/} ,a);
                setListAdapter(adapter); 
    }

try this code it will help you to set song of audio file as ringtone :

File path = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, path.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(path.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);

To search the external sd card for audio files, you can use the following code:

public ArrayList<File> findAllMP3s() {
    static File storage = Environment.getExternalStorageDirectory();
    ArrayList<File> allFiles = new ArrayList<File>();

    recursiveAudioFinder(storage, allFiles);

    return allFiles;
}

public static void recursiveAudioFinder(File directory, ArrayList<File> data>) {
    if(directory == null) {
        return;
    }
    File[] files = directory.listFiles();
    for(File file: files) {
        // Grab audio files, write some filter to check if audio file
        if(file.isFile() && file.getAbsolutePath().endsWith(".mp3")) {
            data.add(file);
        } else if(file.isDirectory()) {
            // Recursive call to find files in sub directories
            recursiveAudioFinder(directory, data);
        }
    }
}

For this to work, the permission READ_EXTERNAL_STORAGE must be added. Choose one of the files in the list, and set the ringtone using RingtoneManager


Need Your Help

Problem converting VS C# 2005 to 2008

visual-studio-2008 visual-studio-2005 xna

I have some projects from XNA 1.0 that I wanted to debug but when I try to convert them to VS 2008 the conversion errors out and only converts the .csproj and .sln file, all other project and resou...

Out of Memory Error Using SBT When Executing Lift Project

scala memory lift sbt

I am using SBT 0.7.7. When I make a change to my Lift project and re-compile via: