本文介绍了机器人 - 如何设置MP3为铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 文件newSoundFile =新的文件(pathFile);
        如果(DebugUtil.DEBUG){
            DebugUtil.logError(新的异常(),newSoundFile.getAbsolutePath());
        }
        ContentValues​​值=新ContentValues​​();
        values​​.put(MediaStore.MediaColumns.DATA,newSoundFile.getAbsolutePath());
        values​​.put(MediaStore.MediaColumns.TITLE,标题);
        values​​.put(MediaStore.MediaColumns.MIME_TYPE,音频/ *);
        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,真);
        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()删除(URI,MediaStore.MediaColumns.DATA += \\+ newSoundFile.getAbsolutePath()+\\,NULL);
        乌里newUri = context.getContentResolver()插入(URI,价值观)。        尝试{
            RingtoneManager.setActualDefaultRingtoneUri(背景下,RingtoneManager.TYPE_RINGTONE,newUri);
            KitKatToast.makeText(上下文,R.string.msg_setAsRingTuneSuccess,KitKatToast.LENGTH_LONG).show();
        }
        赶上(例外五){
            如果(DebugUtil.DEBUG){
                DebugUtil.logError(新的异常(),e.​​toString());
            }
        }


解决方案

我看了很多帖子,但任何人都表现出什么,我其实应该要做的。所以,我决定创建这个完整的答案。唯一想到你居然需要的是一个按钮。
让我们开始。

下面是我的 MainActivity.java 我用

 公共类MainActivity延伸活动{        @覆盖
          公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main2);
        B2 =(按钮)findViewById(R.id.button2);
        b2.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){            意图int​​ent1 =新的Intent();
                intent1.setAction(Intent.ACTION_GET_CONTENT);
                intent1.setType(音频/ *);
                startActivityForResult(Intent.createChooser(intent1,选择声音文件),6);            }        });            }
    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK&放大器;&安培;请求code == 6){
    乌里I = data.getData(); // GETDATA
    字符串s = = i.getPath(); //的getPath
    文件k =新的文件(S); //从路径设置文件如果(S!= NULL){//(file.exists    ContentValues​​值=新ContentValues​​();
       values​​.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
       values​​.put(MediaStore.MediaColumns.TITLE,环);
       values​​.put(MediaStore.MediaColumns.MIME_TYPE,音频/ 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,真);
       values​​.put(MediaStore.Audio.Media.IS_NOTIFICATION,真);
       values​​.put(MediaStore.Audio.Media.IS_ALARM,真);
       values​​.put(MediaStore.Audio.Media.IS_MUSIC,FALSE);       URI的uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
       。getContentResolver()删除(URI,MediaStore.MediaColumns.DATA += \\+ k.getAbsolutePath()+\\,NULL);
    乌里newUri = getContentResolver()插入(URI,价值观)。
       尝试{
           RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this,RingtoneManager.TYPE_RINGTONE,newUri);
       }赶上(的Throwable t)的{       }
}
    }    }    }

最后的真正重要的是你的的Andr​​oidManifest.xml 例如,如果你不添加permision写外部存储您的应用程序会崩溃像我..的xD添加这些permisions

您需要什么:

 <使用许可权的android:NAME =android.permission.WRITE_SETTINGS>< /使用许可权>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
  <使用许可权的android:NAME =android.permission.CHANGE_CONFIGURATION>< /使用许可权>
  <使用许可权的android:NAME =android.permission.MODIFY_AUDIO_SETTINGS>< /使用许可权>

您可以尝试在谷歌我的应用程序播放:

希望帮助....我的问题现在已经解决了!我希望解决您的问题太多:))

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

这篇关于机器人 - 如何设置MP3为铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:00