2017-07-30 150 views
0

点击按钮运行set_ringtone。没有错误,它设置铃声。但是当我第二次点击按钮时,出现错误。Android铃声设置错误

public void set_ringtone(Context context, String ringtone){ 
    File file = new File(Environment.getExternalStorageDirectory(), ringtone + ".mp3"); 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath()); 
    values.put(MediaStore.MediaColumns.TITLE, ringtone); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
    values.put(MediaStore.Audio.AudioColumns.ARTIST, "Halil Öncen"); 
    values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, true); 
    values.put(MediaStore.Audio.AudioColumns.IS_NOTIFICATION, false); 
    values.put(MediaStore.Audio.AudioColumns.IS_ALARM, false); 
    values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, false); 

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()); 
    Uri newUri = context.getContentResolver().insert(uri, values); 
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri); 
} 

logcat中,

07-30 09:14:34.284 5869-5891/? E/SQLiteDatabase: Error inserting _data=/storage/emulated/0/SonyEricssonZilSesi.mp3 title_key=MECY1K9-MMEC[9?M1M9 parent=0 _display_name=SonyEricssonZilSesi.mp3 format=12297 is_music=false is_ringtone=true title=Sony Ericsson Zil Sesi artist_id=2 bucket_id=1389444597 _size=254851 is_alarm=false date_added=1501406074 date_modified=1501406074 mime_type=audio/mp3 media_type=2 storage_id=65537 is_notification=false album_id=4 bucket_display_name=0 
              android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: files._data (code 2067) 
               at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) 
               at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782) 
               at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788) 
               at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) 
               at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474) 
               at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343) 
               at com.android.providers.media.MediaProvider.insertFile(MediaProvider.java:2260) 
               at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2493) 
               at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1892) 
               at android.content.ContentProvider$Transport.insert(ContentProvider.java:266) 
               at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:152) 
               at android.os.Binder.execTransact(Binder.java:674) 
+0

为什么“放置'MediaStore.MediaColumns.DATA'两次? – ZeekHuge

+0

对不起。我现在编辑 –

回答