1
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent); 

1.How can I get an URI of music file using code above?
2.If I have Uri already, how set it as ringtone or alarm? Also tried use:
Convert a file path to Uri in Android

MatL
  • 39
  • 1
  • 9

1 Answers1

1

To get a return result from an activity, use startActivityForResult instead of startActivity.


private int REQUEST_ACTION_PICK = 1;

...

  Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
  // startActivity(intent);
  startActivityForResult(intent, REQUEST_ACTION_PICK);  //fixme

// ...

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_ACTION_PICK && resultCode == RESULT_OK) {
      Uri uri = data.getData();
      Log.d(TAG, uri.toString());
    }
  }


As for 'how set it as ringtone or alarm?', perhaps this might work? Set default alarm sound programatically Android

  • uri2 = data.getData(); from on @Override ActivityResult return: content://media/external/audio/media/174 But something goes wrong RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_ALARM); return null then I call RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_ALARM, uri2); after then I call againg: RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_ALARM); And also return null, it shouldn't return sth like content://media/external/audio/media/174 ? – MatL Mar 30 '20 at 10:40
  • Paste your logcat. Maybe try this: https://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – RandomThoughts2468 Mar 30 '20 at 10:54
  • In button "set song" I do: try{ ringtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_ALARM, uri2); Settings.System.putString(getContentResolver(),Settings.System.ALARM_ALERT,uri2.toString()); ringtone = RingtoneManager.getRingtone(this, uri2); }catch (Throwable t){ } Then in another class whose extends broadcastreciver I call; ringtone.play(); Then I get an error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.Ringtone.play()' on a null object reference – MatL Mar 30 '20 at 15:36
  • Highly possible that the user-supplied uri is the issue. Replace it with the default uri, you should hear something. ``` Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); Ringtone ringtone = RingtoneManager.getRingtone(this, uri); ringtone.play(); ``` Then work your way backwards to determine why the user-uri is null. At this point, I would also assume that you have taken care of 'WRITE_SETTINGS' permissions required for setting the phone's ringtone. – RandomThoughts2468 Mar 30 '20 at 23:33
  • Finally I did it like you said. But not from Mainactivity. In the another class which extends broadcast_receiver. I put the code above, changed only TYPE_ALARM from TYPE_RINGTONE. Also in MainActivity declarated uri2 and ringtone as static and it works! Thanks for your help :D – MatL Mar 31 '20 at 17:59