Re: How to clear mediastore before setting ringtone?
 
Eric Carboni wrote:
When I set a ringtone from my app, it 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 [sic] unique file names
for every sound file, I want to fix this problem.
I found this solution posted in an answer here:
http://stackoverflow.com/questions/4603941/problem-in-setting-audio-file-as-ringtone
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
Do not embed TAB characters in code posted to Usenet.  Use spaces, a maximum of four per indent level.
On some newsreaders (e.g., Google Groups) the TAB character disappears altogether.  On others it expands to some arbitrary width, typically 8 spaces. Either way, it destroys readability.
Also, follow the Java naming conventions, e.g., 'String tag = "CFFS";'.
        
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); }
It's usually not a good idea to continue the main logic flow after an exception.
.... [snip] ...
            
// 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);
You forgot the equals sign.
            
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"
This error message provides the clue: your syntax for the delete was wrong.
In this case, you forgot the equals sign.
-- 
Lew