2012-04-11 71 views
2

我有兩個問題:鈴聲設置了Android 2.3的沉默,但適用於4.0.3

  1. 當我打電話給我的saveas(ressound);的是Android 4.0.3設備上(由stealthcopter.com啓發)鈴聲作爲鈴聲很好地激活,但在2.3.3設備上,選定的鈴聲是無聲的。我的代碼在哪裏出錯?

  2. 我想知道的第二件事是如何刪除文件。更重要的是,如何將其從媒體商店中解脫出來,如果我從sd中刪除它,它會自動發生嗎?


UPDATE:

添加的代碼刪除整個文件夾加內容,做工不錯,乾淨! (見下文)

嘗試調用此代碼時,我按下按鈕:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

但它不會清理出mediastore,它與重新啓動。 我也嘗試了開發工具內的媒體掃描,但這不起作用。他們提到有些應用程序需要重新啓動,但是如何重新啓動負責顯示可用鈴聲列表的服務,而無需重新啓動整個設備?


變量:

String soundname; 
int ressound; 
int savetype; 
String path="/sdcard/mysounds/"; 

savetype = RingtoneManager.TYPE_RINGTONE; 
ressound = R.raw.sound1; 
soundname = "sound1"; 

另存爲(ressound);

//save as 
    public boolean saveas(int ressound){ 
    byte[] buffer=null; 
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
    int size=0; 

    try { 
     size = fIn.available(); 
     buffer = new byte[size]; 
     fIn.read(buffer); 
     fIn.close(); 
    } catch (IOException e) { 
     return false; 
    } 

    String filename= soundname +".mp3"; 

    boolean exists = (new File(path)).exists(); 
    if (!exists){new File(path).mkdirs();} 

    FileOutputStream save; 
    try { 
     save = new FileOutputStream(path+filename); 
     save.write(buffer); 
     save.flush(); 
     save.close(); 
    } catch (FileNotFoundException e) { 
     return false; 
    } catch (IOException e) { 
     return false; 
    }  

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

    File k = new File(path, filename); 

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

    //Insert it into the database 
    Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 

    // set as ringtone 
    RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri); 

    return true; 
    } 

delete();

public void delete() { 
    File a = new File(path, "sound1.mp3"); 
    a.delete(); 
    File b = new File(path, "sound2.mp3"); 
    b.delete(); 
} 

deleteFiles(String path);

public static void deleteFiles(String path) { 
    File file = new File(path); 

    if (file.exists()) { 
     String deleteCmd = "rm -r " + path; 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      runtime.exec(deleteCmd); 
     } catch (IOException e) { } 
    } 
} 
+0

目前市場上有更多的應用程序有這個問題,我不知道它是否可以修復? – John 2012-04-12 19:53:58

+0

也許是因爲它覆蓋,如果文件已經到位。不知道如何修復.... – John 2012-04-12 19:56:27

+0

我做了刪除功能,其中整個地圖加內容被刪除。因此,我檢查了上述假設,沒有運氣。 – John 2012-04-12 19:59:33

回答

1

「android.permission.MODIFY_AUDIO_SETTINGS」 我認爲這個問題解決了一個,沒有發現任何問題,直至現在。

deleteFiles(String path); 是問題2的部分解決方案 重新啓動後,項目消失。