ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何在设置铃声之前清除Mediastore

2019-11-02 07:25:08  阅读:211  来源: 互联网

标签:mediastore ringtone java android


当我从我的应用程序设置铃声一次后,但是再次运行代码时,它会尝试在媒体存储中创建重复条目,这会产生问题.没有为每个声音文件创建单独的唯一文件名,我想解决此问题.

我在以下答案中找到了此解决方案:setting audio file as Ringtone
并试图用它来修复我的

当我在下面的代码中尝试时,出现两个错误.一个是SQLiteException,另一个是RuntimeException,它是由squlite错误引起的,在Java代码之后.

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

错误:

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"

解决方法:

看起来错误是此行:

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

标签:mediastore,ringtone,java,android
来源: https://codeday.me/bug/20191102/1989920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有