简体   繁体   中英

how set SMS ringtone

i am having a small problem with my android application. I have some mp3 files in my app which i want to be able to set as default ringtone or notification.app havent problem to set call ringtone and alaram ringtone but set SMS ringtone doesnt work call ringtone:

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

    }
});

SMS ringtone

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



    }
});

the solution is to copy the sound from the raw folder into the sdcard and from there do the following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM