gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:56 26 4
gpt4 key购买 nike

当我从我的应用程序设置铃声时,一次可以正常工作,但再次运行代码时,它会尝试在媒体存储中创建重复条目,这会产生问题。在不为每个声音文件创建单独的唯一文件名的情况下,我想解决这个问题。

我在此处的答案中找到了此解决方案:setting audio file as Ringtone我正在尝试用它来修复我的问题。

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

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

关于java - 如何在设置铃声之前清除 Mediastore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7302037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com