本文介绍了设置原始资源作为铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看了这2个职位而这其中的但code似乎并没有为我工作。
下面是我的code:
文件newSoundFile =新的文件(/ SD卡/媒体/铃声,myringtone.oog);
乌里穆里= Uri.parse(android.resource://com.pack.android.myapp/R.raw.song1);
ContentResolver的MCR = Main.this.getContentResolver();
AssetFileDescriptor音效档;
尝试{
音效档= mCr.openAssetFileDescriptor(穆里,R);
}赶上(FileNotFoundException异常五){
音效档= NULL;
} 尝试{
字节[] = READDATA新的字节[1024];
FIS的FileInputStream = soundFile.createInputStream();
FOS的FileOutputStream =新的FileOutputStream(newSoundFile);
INT I = fis.read(READDATA); 而(ⅰ!= -1){
fos.write(READDATA,0,I);
I = fis.read(READDATA);
} fos.close();
}赶上(IOException异常IO){
}
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,我的手机铃声);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ 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,真);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,真);
values.put(MediaStore.Audio.Media.IS_ALARM,真);
values.put(MediaStore.Audio.Media.IS_MUSIC,FALSE); URI的uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
乌里newUri = mCr.insert(URI,价值观);
尝试{
RingtoneManager.setActualDefaultRingtoneUri(Main.this,RingtoneManager.TYPE_RINGTONE,newUri);
}赶上(的Throwable t)的{
// Log.d(TAG,catch异常);
}
和我得到一个强制关闭。 logcat的是在这里:
3月6日至三十○日:13:03.731:E / AndroidRuntime(4187):致命异常:主要
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):显示java.lang.NullPointerException
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在com.pack.android.myapp.Main.Music(Main.java:83)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在com.pack.android.myapp.Main $ 2.onClick(Main.java:46)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.view.View.performClick(View.java:2538)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.view.View $ PerformClick.run(View.java:9152)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.os.Handler.handleCallback(Handler.java:587)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.os.Handler.dispatchMessage(Handler.java:92)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.os.Looper.loop(Looper.java:130)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在android.app.ActivityThread.main(ActivityThread.java:3687)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在java.lang.reflect.Method.invokeNative(本机方法)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在java.lang.reflect.Method.invoke(Method.java:507)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
3月6日至三十日:13:03.731:E / AndroidRuntime(4187):在dalvik.system.NativeStart.main(本机方法)
音效档似乎是空。这是为什么?我的歌是song1.mpe并在res下的原始文件夹中。
权限在清单中增加:
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用许可权的android:NAME =android.permission.WRITE_SETTINGS>< /使用许可权>
解决方案
把你的铃声声音的资产文件夹,然后用这个code
URI路径= Uri.parse(android.resource:// yourpackagename /生/ yoursoundfile)
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(),RingtoneManager.TYPE_RINGTONE,
路径);
登录。我(TESTT,铃声设置为资源:+ path.toString());
RingtoneManager.getRingtone(getApplicationContext(),路径)
。玩();
此权限添加到AndroidManifest.xml中:
<使用许可权的android:NAME =android.permission.WRITE_SETTINGS/>
I have read these 2 posts link1 and this one link2 but code does not seem to work for me.
Here is my code:
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");
}
and I am getting a force close. Logcat is here:
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 seems to be null. Why is that? My song is song1.mpe and is in the raw folder under the res.
Permission added in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
解决方案
Put your ringtone sound in assets folder and use this code
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();
Add this permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
这篇关于设置原始资源作为铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!