Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to clear mediastore before setting ringtone?

Reply
Thread Tools

How to clear mediastore before setting ringtone?

 
 
Eric Carboni
Guest
Posts: n/a
 
      09-04-2011
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 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/4...le-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
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().open InputStream(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(output File.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(1196:
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"
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-04-2011
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/4...le-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().open InputStream(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(1196:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
clear() before delete? --hash_map newbie C++ 4 04-06-2007 10:48 PM
Unrecognized element 'add' after <clear></clear> InvalidLastName ASP .Net Web Services 3 03-06-2007 03:07 AM
Before Sunset & Before Sunrise matt r DVD Video 2 11-25-2004 08:37 AM



Advertisments