0

以下のコードのように生のファイルから着信音を追加すると、機能します。追加したら、Android System Ringtones リストを開くことができ、そこに表示されるので、私 (または他のアプリ) はそれを使用できます。問題は、電話を再起動すると、リストからそのエントリが失われることです。では、着信音を永続的に追加する方法はありますか? ありがとう

このコードを使用して追加します。

private void AddRingTone() //I assume that sdcard directory exists and it is empty
{  //We first copy the raw resource to sdcard:
   String sPath = Environment.getExternalStorageDirectory() + "/AnyPath"; //AnyPath already exists.        
   File newSoundFile = new File(sPath, "MyRingtone");
   Uri mUri = Uri.parse("android.resource://" + getPackageName()+ "/" + R.raw.myringtone);
   AssetFileDescriptor soundFile;
   try 
   {  soundFile= getContentResolver().openAssetFileDescriptor(mUri, "r");
   }catch (FileNotFoundException e)
   {  soundFile=null;   
      MessageBox("Cannot open " + mUri.toString());
   }
   try
   {  byte[] readData = new byte[1024];
      FileInputStream fis = soundFile.createInputStream();
      FileOutputStream fos = new FileOutputStream(newSoundFile);
      int i = fis.read(readData);
      while (i != -1)
      { fos.write(readData, 0, i);
        i = fis.read(readData);
      }
      fos.close();
   }catch(IOException io)
   {  MessageBox("RingtoneManager:\n" + io.toString());  
   }


   //Now we add it to the system list:
   ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "my ring tone");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
   values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
   values.put(MediaStore.Audio.Media.ARTIST, "me");
   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
   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(newSoundFile.getAbsolutePath());
   Uri newUri = getContentResolver().insert(uri, values);

}

4

1 に答える 1

1

消えてしまうのは、正しい場所に保管されていないからです!

Android がメディア ファイルを監視する公式の場所は、 /sdcard/media/audio/Ringtonesまたは/sdcard/media/audio/notificationsのいずれか最適な場所にあります。

Android は呼び出されたディレクトリについてまったく認識AnyPathしておらず、メディア スキャナはそれを検出しません!

于 2013-01-23T16:55:12.410 に答える