How to package a music file in a program and set it as a ringtone in Android

Source: Internet
Author: User
This article describes how to package a music file into a program and set it as a ringtone.

 

First, put our ringtone file in the Res/raw directory. If no, create a raw directory.

 

Put the canonworkflow file in the raw directory.

 

 

Then read the file in the program:

The code is

Getresources (). openrawresource (R. Raw. Canon ); 

Then write the read content to the sdcard.

 

The program can directly set a file on the sdcard as a ringtone.

 

The specific implementation code is as follows:

 

Setring. Java

Package ZY. ring; <br/> Import Java. io. file; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import android. app. activity; <br/> Import android. content. contentvalues; <br/> Import android. media. ringtonemanager; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. provider. mediastore; <br/> Import android. util. log; <br /> Import android. widget. toast; <br/> public class setring extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> inputstream = NULL; </P> <p> try {<br/> inputstream = getresources (). openrawresource (R. raw. canon); <Br/> byte [] reader = new byte [inputstream. Available ()]; <br/> while (inputstream. Read (Reader )! =-1) {}< br/> writefile (reader, "/sdcard/canoncard"); <br/> log. I ("setring", "success"); <br/>} catch (ioexception e) {<br/> log. I ("setring", E. getmessage (); <br/>}finally {<br/> If (inputstream! = NULL) {<br/> try {<br/> inputstream. close (); <br/>}catch (ioexception e) {<br/> log. I ("setring", E. getmessage (); <br/>}</P> <p> setmyringtone (); </P> <p >}< br/> Public void writefile (byte [] STR, string path) <br/>{< br/> file; <br/> fileoutputstream out; <br/> try {<br/> // create a file <br/> file = new file (PATH); <br/> file. createnewfile (); <br/> // open the outputstream of the file <br/> out = new fileoutputstream (File ); </P> <p> // convert the string to a byte array and write it to the file. <br/> out. write (STR); <br/> // close the outputstream of the file <br/> out. close (); <br/>}catch (ioexception e) {<br/> // print the error message to logcat <br/> displaytoast (E. tostring (); </P> <p >}< br/>}</P> <p> Public void setmyringtone () <br/>{< br/> File K = new file ("/sdcard/canonloud"); // set the song path <br/> contentvalues values = new contentvalues (); <br/> values. put (mediastore. mediacolumns. data, K. getabsolutepath (); <br/> values. put (mediastore. mediacolumns. title, K. getname (); <br/> values. put (mediastore. mediacolumns. size, 8474325); <br/> values. put (mediastore. mediacolumns. mime_type, "audio/MP3"); <br/> values. put (mediastore. audio. media. artist, "Madonna"); <br/> values. put (mediastore. audio. media. duration, 230); <br/> values. put (mediastore. audio. media. is_ringtone, true); <br/> values. put (mediastore. audio. media. is_notification, false); <br/> values. put (mediastore. audio. media. is_alarm, false); <br/> values. put (mediastore. audio. media. is_music, false); <br/> // insert it into the database <br/> URI uri = mediastore. audio. media. getcontenturiforpath (K. getabsolutepath (); <br/> URI newuri = This. getcontentresolver (). insert (Uri, values); <br/> ringtonemanager. setactualdefaultringtoneuri (this, ringtonemanager. type_ringtone, newuri); <br/>;}< br/> Public void displaytoast (string Str) <br/>{< br/> toast. maketext (this, STR, toast. length_short ). show (); <br/>}< br/>} 

 

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "ZY. ring "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". setring "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "3"/> <br/> <uses-Permission Android: Name = "android. permission. write_settings "> </uses-Permission> <br/> </manifest> 

Permission required

<Uses-Permission Android: Name = "android. Permission. write_settings"> </uses-Permission> 

The program can copy the canoncard in the package to the sdcard and set it as the default ringtone.

 

Of course, you can also copy this file to your own private directory data/ZY. Ring /.

 

 

At last, we opened the phone setting window, and we can see our canonboarding set as the default ringtone.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.