gpt4 book ai didi

android - 如何将媒体商店中没有的音乐文件设置为铃声

转载 作者:行者123 更新时间:2023-11-29 01:50:26 25 4
gpt4 key购买 nike

在 Jelly Bean 级别 18 之前,我可以使用不在媒体商店中的音乐文件(例如,文件夹中的 private .mp3 文件包含 .nomedia 文件,所以 Media Scanner 不扫描这个文件夹并且不知道这个文件)但是从 Android 4.3(在 Nexus 4 上测试)我不能这样做,它只适用于已经被 Media Scanner 扫描的音乐文件。

这个问题的真正原因是我不能用 MediaColumns 插入 ContentValues。DATA 是一个没有被 Media Scanner 扫描的文件的绝对路径,insert 方法总是返回 null。

Uri newUri = getCR().insert(uri, contentValues); // returns null in Android 4.3 

有没有人有使用私有(private)文件(未扫描且未被 Media Scanner 识别)作为铃声的解决方法?

我是这样设置铃声的:

    File ringtoneFile = new File(audio.getPath());

ContentValues cv = new ContentValues();
cv.put(MediaColumns.DATA, ringtoneFile.getAbsolutePath());
cv.put(MediaColumns.TITLE, audio.getTitle());

cv.put(MediaColumns.MIME_TYPE, "audio/*");
if (audio.getArtist() != null)
cv.put(Media.ARTIST, audio.getArtist());

cv.put(Media.IS_RINGTONE, true);
cv.put(Media.IS_NOTIFICATION, false);
cv.put(Media.IS_ALARM, false);
cv.put(Media.IS_MUSIC, false);

Uri uri = Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
Uri newUri = getCR().insert(uri, cv); ////return null in Android 4.3
if (newUri == null)
{
Cursor c = getCR().query(uri, new String[] { Media._ID }, Media.DATA + "=?",
new String[] { ringtoneFile.getAbsolutePath() }, null);
long id = -1;
if (c != null && c.moveToFirst())
{
id = c.getLong(c.getColumnIndex(Media._ID));
newUri = Uri.parse(uri.toString() + "/" + id);
c.close();
}
}

if (newUri != null)
{
RingtoneManager.setActualDefaultRingtoneUri(getAppContext(), RingtoneManager.TYPE_RINGTONE, newUri);
}

最佳答案

我尝试了一些东西,它对我有用。希望对您也有帮助。

我正在使用以下方法获取设备中所有可用的歌曲:

public static long [] getAllSongs(Context context) {
//System.out.println("In get All Songs Method");
Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.IS_MUSIC +"!= 0",
null, MediaStore.Audio.Media.TITLE + " ASC");
try {
if (c == null || c.getCount() == 0) {
return null;
}
int len = c.getCount();
long [] list = new long[len];
for (int i = 0; i < len; i++) {
c.moveToNext();
list[i] = c.getLong(0);
//System.out.println("ID IS: "+c.getLong(0));
}
//System.out.println("Total Songs are: );

// ALL_SONGS_ID = list;
return list;
} finally {
if (c != null) {
c.close();
}
}
}

获取歌曲后,我选择声音文件并将其设置为铃声,代码如下:

 static void setRingtone(Context context, long id) {
ContentResolver resolver = context.getContentResolver();
// Set the flag in the database to mark this as a ringtone
Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
try {
ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
values.put(MediaStore.Audio.Media.IS_ALARM, "1");
resolver.update(ringUri, values, null, null);
} catch (UnsupportedOperationException ex) {
// most likely the card just got unmounted
Log.e("Music app", "couldn't set ringtone flag for id " + id);
return;
}

String[] cols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};

String where = MediaStore.Audio.Media._ID + "=" + id;
Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols, where , null, null);
try {
if (cursor != null && cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
String message = context.getString(R.string.ringtone_set, cursor.getString(2));
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
} finally {
if (cursor != null) {
cursor.close();
}
}

在上面的方法中id是所选歌曲的Song id。

希望对您有所帮助。如果您没有找到解决方案,请随意询问。

享受编码... :)

关于android - 如何将媒体商店中没有的音乐文件设置为铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459808/

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