android - 铃声在 android 2.3 上设置为静音,但在 4.0.3 上有效

标签 android delete-file mediastore ringtone

我有两个问题:

  1. 当我在 android 4.0.3 设备上调用我的 saveas(ressound);(灵感来自 stealthcopter.com)时,铃声被很好地激活为铃声,但在 2.3. 3 设备选择的铃声是无声的。我的代码哪里出错了?

  2. 我想知道的第二件事是如何删除文件。更重要的是,如何将其从媒体存储中取出,如果我将其从 sd 中删除会自动发生吗?


更新:

添加了删除整个文件夹和内容的代码,效果很好,很干净! (见下文)

尝试在我按下按钮时调用此代码:

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

saveas(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;
    }

删除();

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

deleteFiles(字符串路径);

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) { }
    }
}

最佳答案

“android.permission.MODIFY_AUDIO_SETTINGS” 我认为这解决了问题一,直到现在没有发现任何问题。

删除文件(字符串路径); 是问题2的部分解决方案 重新启动后,项目消失了。

关于android - 铃声在 android 2.3 上设置为静音,但在 4.0.3 上有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105877/

相关文章:

android - 为什么我在通过蓝牙发送 Arduino 数据时在 Android 应用程序的开头看到意外数据?

切换 fragment 后,Android TabLayout 不再显示内容

django - 在 Django 中删除上传的文件

android - 在 Lollipop 及以上版本上仅获得一次 sdcard 窗口的权限

android - 录制的视频无法立即从文件位置读取?

android - 是否可以使用 phonegap 加 sencha touch 开发应用程序

android - 如何在 Google Drive Android 中备份房间数据库?

git - 从所有提交中删除旧的别名和电子邮件

android - 如何将音频文件从原始文件夹保存到手机

android - 是否可以使用 MediaStore.VOLUME_EXTERNAL 获取 WhatsApp 媒体文件