我已经阅读了这两个帖子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: main06-30 03:13:03.731: E/AndroIDRuntime(4187): java.lang.NullPointerException06-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.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似乎是null.这是为什么?我的歌是song1.mpe,位于res下的原始文件夹中.
清单中添加的权限:
<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission androID:name="androID.permission.WRITE_SETTINGS"></uses-permission>
解决方法:
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 – 将原始资源设置为铃声全部内容,希望文章能够帮你解决android – 将原始资源设置为铃声所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)