gpt4 book ai didi

android - 将原始资源设置为铃声

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:07 27 4
gpt4 key购买 nike

我已经阅读了这 2 个帖子 link1还有这个link2但代码似乎对我不起作用。

这是我的代码:

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog");
Uri mUri = Uri.parse("android.resource://com.pack.android.myapp/R.raw.song1");
ContentResolver mCr = Main.this.getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}

try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);

while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}

fos.close();
} catch (IOException io) {
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);


try {
RingtoneManager.setActualDefaultRingtoneUri(Main.this, RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {
// Log.d(TAG, "catch exception");
}

我正在逼近。 Logcat 在这里:

06-30 03:13:03.731: E/AndroidRuntime(4187): FATAL EXCEPTION: main
06-30 03:13:03.731: E/AndroidRuntime(4187): java.lang.NullPointerException
06-30 03:13:03.731: E/AndroidRuntime(4187): at com.pack.android.myapp.Main.Music(Main.java:83)
06-30 03:13:03.731: E/AndroidRuntime(4187): at com.pack.android.myapp.Main$2.onClick(Main.java:46)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.view.View.performClick(View.java:2538)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.view.View$PerformClick.run(View.java:9152)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.os.Handler.handleCallback(Handler.java:587)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.os.Looper.loop(Looper.java:130)
06-30 03:13:03.731: E/AndroidRuntime(4187): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-30 03:13:03.731: E/AndroidRuntime(4187): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 03:13:03.731: E/AndroidRuntime(4187): at java.lang.reflect.Method.invoke(Method.java:507)
06-30 03:13:03.731: E/AndroidRuntime(4187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-30 03:13:03.731: E/AndroidRuntime(4187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-30 03:13:03.731: E/AndroidRuntime(4187): at dalvik.system.NativeStart.main(Native Method)

soundFile 似乎为空。这是为什么?我的歌曲是song1.mpe,在res下的raw文件夹里。

list 中添加的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>

最佳答案

将您的铃声放在 Assets 文件夹中并使用此代码

Uri path = Uri.parse("android.resource://yourpackagename/raw/yoursoundfile")
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
path);
Log .i("TESTT", "Ringtone Set to Resource: "+ path.toString());
RingtoneManager.getRingtone(getApplicationContext(), path)
.play();

将此权限添加到 AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

关于android - 将原始资源设置为铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270429/

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