问题描述:

File newSoundFile = new File("pathFile");

if (DebugUtil.DEBUG) {

DebugUtil.logError(new Exception(), newSoundFile.getAbsolutePath());

}

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, "Title");

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");

values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());

values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);

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

Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());

context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newSoundFile.getAbsolutePath() + "\"", null);

Uri newUri = context.getContentResolver().insert(uri, values);

try {

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

KitKatToast.makeText(context, R.string.msg_setAsRingTuneSuccess, KitKatToast.LENGTH_LONG).show();

}

catch (Exception e) {

if (DebugUtil.DEBUG) {

DebugUtil.logError(new Exception(), e.toString());

}

}

网友答案:

I saw many posts but anyone showed what i should actually have to do. So i decided to create this complete answer. The only think you actually need is a button. Lets start.

Here is my MainActivity.java which i used

        public class MainActivity extends Activity {     

        @Override
          public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

            Intent intent1 = new Intent();  
                intent1.setAction(Intent.ACTION_GET_CONTENT);  
                intent1.setType("audio/*"); 
                startActivityForResult(Intent.createChooser(intent1, "Choose Sound File"), 6);



            }

        });

            }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode ==RESULT_OK&&requestCode==6){
    Uri i = data.getData(); //getDATA
    String s = = i.getPath(); //getPath
    File k = new File(s); //set File from path

if(s!=null){  //(file.exists

    ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "ring");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
       values.put(MediaStore.MediaColumns.SIZE, k.length());
       values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
       values.put(MediaStore.Audio.Media.IS_ALARM, true);
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);

       Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
       getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
    Uri newUri = getContentResolver().insert(uri, values);


       try {
           RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
       } catch (Throwable t) {

       }    
}


    }



    }



    } 

Lastly its really important to add those permisions in your AndroidManifest.xml for example if you dont add the permision to write external storage your app will crash like mine.. xD

What you need:

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>

You can try my app on Google Play : BackAtel Audio Manager

Hope that helps.... my problem is now solved!! i hope that i solved your problem too :))

相关阅读:
Top