2012-04-11 68 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的部分解决方案 重新启动后,项目消失。