2013-01-23 26 views
0

我從以下代碼的原始文件中添加鈴聲,並且它可以工作。 一旦我添加了它,我就可以打開Android系統鈴聲列表並且它出現在那裏,所以我(或任何其他應用程序)可以使用它。 問題是,如果我重新啓動手機,那麼從列表中的條目已經消失。 那麼,有沒有辦法永久添加鈴聲? 感謝重新啓動時,我添加的鈴聲消失

我使用此代碼添加:

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

}

回答

1

它消失的原因是其不存儲在正確的地方!

Android保持媒體文件標籤的官方位置位於/sdcard/media/audio/Ringtones/sdcard/media/audio/notifications,以最合適的爲準!

Android知道零有關一個名爲AnyPath的目錄,並且媒體掃描程序不會提取它!

+0

對不起,仍然沒有工作:-(我不知道我在做什麼錯 – Ton

+0

是的,它工作。對不起,我不得不重新啓動手機,但也等待了漫長的過程「搜索媒體... 「我認爲它已經在系統領域,因此不需要重新搜索。 – Ton

相關問題