如何设置铃声之前清除Mediastore如何设置、铃声、Mediastore

2023-09-06 17:21:14 作者:久孤

当我设置的铃声从我的应用程序运行一次,但再次运行code时,它试图创建的媒体商店,这将创建一个问题重复的条目。如果没有每个声音文件创建单独唯一的文件名,我想解决这个问题。

我发现这个解决方案张贴在这里的答案:问题制定的音频文件作为铃声 ,我试图用它来解决我的。

当我尝试在我下面的code,我得到两个错误。一个是SQLiteException而另一个是它是由squlite错误,这是经过了Java code进行的RuntimeException。

 字符串变量=CFFS;


    文件DIR =新的文件(Environment.getExternalStorageDirectory()+/铃声); //设置基地DIR其中,新的铃声将生活
    dir.mkdirs(); //创建,如果董事会不存在

    文件OUTPUTFILE =新的文件(目录,大学生打Song.mp3的); //定义了新的输出文件


    乌里inURI = NULL;
    尝试 {
        inURI = Uri.parse(getIntent()getStringExtra(com.carboni.fightsongs.FILE_RES_ID)。);
    }赶上(例外五){
        e.printStackTrace();
        Log.e(TAG,无法获取URI+ E);
    }


    //如果我们不分析一个很好的URI那么不执行低于code
    如果(inURI!= NULL){
        在的InputStream = NULL;
        //获取输入流
        尝试在{=新的BufferedInputStream(this.getContentResolver()openInputStream(inURI)); }
        赶上(例外五){Log.e(TAG,异常获取输入流+ E); }

        //获取输出流
        出的OutputStream = NULL;
        尝试{OUT =新的FileOutputStream(OUTPUTFILE); }
        赶上(例外五){Log.e(TAG,异常得到输出流+ E); }

        //同样,如果我们没有2好手柄那就不要尝试读/写他们
        如果((在= NULL)及!及(OUT = NULL)!){

            byte []的BUF =新的字节[1024]; //定义我们的缓冲区大小
            INT读取动作= 0;
            而(读取动作> = 0){
                尝试 {
                    读取动作= in.read(buf中,0,buf.length); //读取最大1024字节
                    如果(读取动作大于0)
                        out.write(BUF); //写,如果我们有一个很好的读取缓冲区,以新的文件
                }赶上(例外五){
                    Log.e(TAG,异常读数+ E);
                    e.printStackTrace();
                }
            }
        }
        //关闭了把手,并继续
        尝试 {
            附寄();
            out.close();
        }
        赶上(例外五){Log.e(TAG,异常关闭流+ E); }

        ContentValues​​ V =新ContentValues​​();
        v.put(MediaStore.MediaColumns.DATA,outputFile.getAbsolutePath());
        v.put(MediaStore.MediaColumns.TITLE,学院橄榄球战斗歌曲);
        v.put(MediaStore.MediaColumns.SIZE,outputFile.length());
        v.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP3);
        v.put(MediaStore.Audio.Media.IS_RINGTONE,真正的);

        乌里普里= MediaStore.Audio.Media.getContentUriForPath(outputFile.getAbsolutePath());

        //每一次删除的条目,所以我们没有得到重复条目,并有一个问题设置第二次
        。getContentResolver()删除(普瑞,MediaStore.MediaColumns.DATA +\+ outputFile.getAbsolutePath()+\,NULL);

        乌里鹦鹉= this.getContentResolver()插入(普瑞,V)。

        Log.i(TAG,设置铃声的URI为+努里);

        //设置铃声
        RingtoneManager.setActualDefaultRingtoneUri(这一点,RingtoneManager.TYPE_RINGTONE,努里);
        Toast.makeText(这一点,铃声设置,Toast.LENGTH_LONG).show();
 
从铃声多多下载到手机系统的铃声怎么删除

ERROR:

  09-03 14:16:08.343:ERROR / DatabaseUtils(11968):android.database.sqlite.SQLiteException:近到/ mnt / SD卡/铃声/学院战斗歌曲。 MP3:语法错误:在编制:DELETE FROM audio_meta WHERE _data到/ mnt / SD卡/铃声/学院扑灭Song.mp3的
 

解决方案

看起来错误的是这一行:

  getContentResolver()删除(普瑞,MediaStore.MediaColumns.DATA +\+ outputFile.getAbsolutePath()+= \,NULL);
 

When I set a ringtone from my app works once, but when running the code again, it tries to create a duplicate entry in the media store, which creates problems. Without creating seperate unique file names for every sound file, I want to fix this problem.

I found this solution posted in an answer here: Problem in setting audio file as Ringtone and am trying to use that to fix mine.

When I try it in my code below, I get two errors. One is an SQLiteException and the other is a RuntimeException which is caused by the squlite error, which is after the Java code.

String TAG = "CFFS";


    File dir = new File(Environment.getExternalStorageDirectory()+ "/ringtones"); // Set base DIR where new ringtone will live
    dir.mkdirs(); // create if directors don't exist

    File outputFile = new File(dir, "College Fight Song.mp3"); // Define out new output file


    Uri inURI = null;
    try {
        inURI = Uri.parse(getIntent().getStringExtra("com.carboni.fightsongs.FILE_RES_ID"));
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Could not get URI " + e);
    }


    // If we didn't parse a good URI then don't execute the code below
    if (inURI != null) {
        InputStream in = null;
        // Get the input stream
        try { in = new BufferedInputStream(this.getContentResolver().openInputStream(inURI));           }
        catch (Exception e) { Log.e(TAG, "Exception getting input stream " + e); }

        // Get the output stream
        OutputStream out = null;
        try { out = new FileOutputStream(outputFile); } 
        catch (Exception e) { Log.e(TAG, "Exception getting output stream " + e); }

        // Again, if we don't have 2 good handles then don't try to read/write them
        if ((in != null) && (out != null)) {

            byte[] buf = new byte[1024]; // Define our buffer size
            int bytesRead = 0;
            while (bytesRead >= 0) {
                try {
                    bytesRead = in.read(buf, 0, buf.length); // Read max of 1024 bytes
                    if (bytesRead > 0)
                        out.write(buf); // Write buffer to new file if we got a good read
                } catch (Exception e) {
                    Log.e(TAG,"Exception reading " + e);
                    e.printStackTrace();
                }                   
            }
        }
        // Close out handles and proceed
        try {
            in.close();
            out.close();
        }
        catch (Exception e) { Log.e(TAG, "Exception closing streams " + e); }

        ContentValues v = new ContentValues();
        v.put(MediaStore.MediaColumns.DATA, outputFile.getAbsolutePath());
        v.put(MediaStore.MediaColumns.TITLE, "College Football Fight Song");
        v.put(MediaStore.MediaColumns.SIZE, outputFile.length());
        v.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        v.put(MediaStore.Audio.Media.IS_RINGTONE, true);

        Uri pURI = MediaStore.Audio.Media.getContentUriForPath(outputFile.getAbsolutePath());

        // remove entry every time so we don't get duplicate entries and have a problem setting a 2nd time
        getContentResolver().delete(pURI, MediaStore.MediaColumns.DATA + "\"" + outputFile.getAbsolutePath() + "\"", null);

        Uri nURI = this.getContentResolver().insert(pURI, v);

        Log.i(TAG, "Setting ringtone URI to " + nURI);

        // Set ringtone
        RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, nURI);
        Toast.makeText(this, "Ringtone set", Toast.LENGTH_LONG).show();

ERROR:

09-03 14:16:08.343: ERROR/DatabaseUtils(11968): android.database.sqlite.SQLiteException: near ""/mnt/sdcard/ringtones/College Fight Song.mp3"": syntax error: , while compiling: DELETE FROM audio_meta WHERE _data"/mnt/sdcard/ringtones/College Fight Song.mp3"

解决方案

It looks like the error is this line:

getContentResolver().delete(pURI, MediaStore.MediaColumns.DATA + "\"" + outputFile.getAbsolutePath() + "=\"", null);