2014-07-27 86 views
2

我的Android應用程序有一個小問題。我在我的應用程序的一些MP3文件,我希望能夠將其設置爲默認鈴聲或notification.app還沒有問題設置來電鈴聲和鈴聲alaram但設置短信鈴聲不工作 電話鈴聲:如何設置短信鈴聲

public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     btn3.setBackgroundResource(R.drawable.ok); 
     File localFile1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bomb/"); 
     Uri mUri = Uri.parse("android.resource://ir.bazisong/"+R.raw.bababanana); 
     ContentResolver mCr = bombsound2.this.getContentResolver(); 
     AssetFileDescriptor soundFile; 
     try { 
       soundFile= mCr.openAssetFileDescriptor(mUri, "r"); 
      } catch (FileNotFoundException e) { 
       soundFile=null; 
      } 

      try { 
       byte[] readData = new byte[1024]; 
       FileInputStream fis = soundFile.createInputStream(); 
       FileOutputStream fos = new FileOutputStream(localFile1); 
       int i = fis.read(readData); 

       while (i != -1) { 
       fos.write(readData, 0, i); 
       i = fis.read(readData); 
       } 

       fos.close(); 
      } catch (IOException io) { 
      } 


      ContentValues localContentValues = new ContentValues(); 
      localContentValues.put("_data", localFile1.getAbsolutePath()); 
      localContentValues.put("title", "bomb"); 
      localContentValues.put("mime_type", "audio/ogg"); 
      localContentValues.put("artist", "cssounds "); 
      localContentValues.put("is_ringtone", Boolean.valueOf(true)); 
      localContentValues.put("is_notification", Boolean.valueOf(false)); 
      localContentValues.put("is_alarm", Boolean.valueOf(false)); 
      localContentValues.put("is_music", Boolean.valueOf(false)); 
      Uri localUri1 = MediaStore.Audio.Media.getContentUriForPath(localFile1.getAbsolutePath()); 
      Uri localUri2 = bombsound2.this.getContentResolver().insert(localUri1, localContentValues); 
      RingtoneManager.setActualDefaultRingtoneUri(bombsound2.this.getApplication(), 1, localUri2); 
      ((Vibrator)bombsound2.this.getSystemService("vibrator")).vibrate(50L); 
      Toast.makeText(bombsound2.this.getApplicationContext(), "زنگ تماس انتخاب شد", 1).show(); 

    } 
}); 

短信鈴聲

@Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     btn4.setBackgroundResource(R.drawable.ok); 

     File localFile1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bomb/"); 
     Uri mUri = Uri.parse("android.resource://ir.bazisong/"+R.raw.bababanana); 
     ContentResolver mCr = bombsound2.this.getContentResolver(); 
     AssetFileDescriptor soundFile; 
     try { 
       soundFile= mCr.openAssetFileDescriptor(mUri, "r"); 
      } catch (FileNotFoundException e) { 
       soundFile=null; 
      } 

      try { 
       byte[] readData = new byte[1024]; 
       FileInputStream fis = soundFile.createInputStream(); 
       FileOutputStream fos = new FileOutputStream(localFile1); 
       int i = fis.read(readData); 

       while (i != -1) { 
       fos.write(readData, 0, i); 
       i = fis.read(readData); 
       } 

       fos.close(); 
      } catch (IOException io) { 
      } 


      ContentValues localContentValues = new ContentValues(); 
      localContentValues.put("_data", localFile1.getAbsolutePath()); 
      localContentValues.put("title", "bomb"); 
      localContentValues.put("mime_type", "audio/ogg"); 
      localContentValues.put("artist", "cssounds "); 
      localContentValues.put("is_ringtone", Boolean.valueOf(false)); 
      localContentValues.put("is_notification", Boolean.valueOf(true)); 
      localContentValues.put("is_alarm", Boolean.valueOf(false)); 
      localContentValues.put("is_music", Boolean.valueOf(false)); 
      Uri localUri1 = MediaStore.Audio.Media.getContentUriForPath(localFile1.getAbsolutePath()); 
      Uri localUri2 = bombsound2.this.getContentResolver().insert(localUri1, localContentValues); 
      RingtoneManager.setActualDefaultRingtoneUri(bombsound2.this.getApplication(), 1, localUri2); 
      ((Vibrator)bombsound2.this.getSystemService("vibrator")).vibrate(50L); 
      Toast.makeText(bombsound2.this.getApplicationContext(), "زنگ پیام انتخاب شد", 1).show(); 



    } 
}); 

回答

0

的解決方案是將聲音從原始文件夾複製到SD卡,並從那裏做以下

File k = new File(path, filename); 

ContentValues values = new ContentValues(); 
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
values.put(MediaStore.MediaColumns.TITLE, "TwiAppclip"); 
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*"); 
values.put(MediaStore.Audio.Media.ARTIST, "cssounds "); 
values.put(MediaStore.Audio.Media.IS_RINGTONE, false); 
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
values.put(MediaStore.Audio.Media.IS_ALARM, false); 
values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k 
    .getAbsolutePath()); 
//do a delete here before inserting 
Uri newUri = getApplicationContext().getContentResolver().insert(uri, values); 

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