如何將音樂文件打包到程序中並設置為鈴聲 – Android移動開發技術文章_手機開發 Android移動開發教學課程

canon.mp3文件放到raw目錄下
 
 
然後在程序裡讀取這個文件:
代碼為

view plaincopy to clipboardprint?
getResources().openRawResource(R.raw.canon);  
getResources().openRawResource(R.raw.canon);
然後將讀出的內容寫到sdcard上
 
 
程序能直接把sdcard上的一個文件設置為鈴聲。
 
具體的實現代碼如下
 
setring.java

view plaincopy to clipboardprint?
package zy.ring; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import android.app.Activity; 
import android.content.ContentValues; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.widget.Toast; 
public class setring extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        InputStream inputStream = null;   
        
        try {   
            inputStream = getResources().openRawResource(R.raw.canon);   
            byte[] reader = new byte[inputStream.available()];   
            while (inputStream.read(reader) != -1) {}   
            writefile(reader,"/sdcard/canon.mp3"); 
            Log.i("setring", "success"); 
        } catch(IOException e) {   
            Log.i("setring", e.getMessage());   
        } finally {   
            if (inputStream != null) {   
                try {   
                    inputStream.close();   
                } catch (IOException e) {   
                    Log.i("setring", e.getMessage());   
                }   
            }   
        }   
         
         setMyRingtone(); 
         
    }   
    public void writefile(byte[] str,String path ) 
    { 
        File file; 
        FileOutputStream out; 
         try { 
             //創建文件  
             file = new File(path); 
             file.createNewFile(); 
             //打開文件file的OutputStream  
             out = new FileOutputStream(file); 
              
             //將字符串轉換成byte數組寫入文件  
             out.write(str); 
             //關閉文件file的OutputStream  
             out.close();  
         } catch (IOException e) { 
             //將出錯信息打印到Logcat  
          DisplayToast(e.toString()); 
              
         } 
    }  
         
      
     
   public void setMyRingtone() 
    { 
   File k = new File("/sdcard/canon.mp3"); // 設置歌曲路徑  
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
    values.put(MediaStore.MediaColumns.TITLE, k.getName()); 
    values.put(MediaStore.MediaColumns.SIZE, 8474325); 
    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(k.getAbsolutePath()); 
    Uri newUri = this.getContentResolver().insert(uri, values); 
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri); 
    ;} 
    public void DisplayToast(String str)   
    {   
  Toast.makeText(this,str,Toast.LENGTH_SHORT).show();   
    } 

package zy.ring;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
public class setring extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InputStream inputStream = null; 
      
        try { 
            inputStream = getResources().openRawResource(R.raw.canon); 
            byte[] reader = new byte[inputStream.available()]; 
            while (inputStream.read(reader) != -1) {} 
            writefile(reader,"/sdcard/canon.mp3");
            Log.i("setring", "success");
        } catch(IOException e) { 
            Log.i("setring", e.getMessage()); 
        } finally { 
            if (inputStream != null) { 
                try { 
                    inputStream.close(); 
                } catch (IOException e) { 
                    Log.i("setring", e.getMessage()); 
                } 
            } 
        } 
       
         setMyRingtone();
       
    } 
    public void writefile(byte[] str,String path )
    {
     File file;
     FileOutputStream out;
      try {
             //創建文件
       file = new File(path);
             file.createNewFile();
             //打開文件file的OutputStream
             out = new FileOutputStream(file);
            
             //將字符串轉換成byte數組寫入文件
             out.write(str);
             //關閉文件file的OutputStream
             out.close();
         } catch (IOException e) {
             //將出錯信息打印到Logcat
          DisplayToast(e.toString());
            
         }
    }
       
    
   
   public void setMyRingtone()
    {
   File k = new File("/sdcard/canon.mp3"); // 設置歌曲路徑
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, k.getName());
    values.put(MediaStore.MediaColumns.SIZE, 8474325);
    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(k.getAbsolutePath());
    Uri newUri = this.getContentResolver().insert(uri, values);
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
    ;}
    public void DisplayToast(String str) 
    { 
  Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); 
    }
}
 
AndroidManifest.xml

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="zy.ring" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".setring" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> 
</manifest>  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="zy.ring"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".setring"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
</manifest>
需要加入權限

view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
程序運行就能將包中的canon.mp3拷貝到sdcard並且設置為默認鈴聲瞭
 
當然,你也可以拷貝這個文件到自己的私有目錄data/data/zy.ring/路徑下也可以
 
 
最後我們打開手機的設置鈴聲界面,可以看到我們的canon.mp3設置成瞭默認的鈴聲

作者“技術人生”

發佈留言