Android Froyo 设置铃声

发布于 2024-10-03 06:23:39 字数 2645 浏览 3 评论 0原文

我编写了一段代码来从 Android 2.1 中的 URL 添加铃声。在 Froyo 中它根本不想工作。

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
.fromFile(file)));

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA,
file.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, filenameBase);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");         
values.put(AudioColumns.IS_RINGTONE, true);
values.put(AudioColumns.IS_NOTIFICATION, false);
values.put(AudioColumns.IS_ALARM, false);
values.put(AudioColumns.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(file
.getAbsolutePath());

Uri newUri = RingtoneModule.this.getContentResolver()
.insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
RingtoneModule.this, RingtoneManager.TYPE_RINGTONE,
newUri);

当我插入时,我得到了这个异常:

11-17 09:54:51.802: ERROR/DatabaseUtils(379): java.lang.IllegalStateException: Unknown URL: content://media/external/audio/albumart/-1
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.query(MediaProvider.java:1666)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.getAlbumArtOutputUri(MediaProvider.java:2983)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.makeThumbInternal(MediaProvider.java:3192)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.getThumb(MediaProvider.java:3070)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2029)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1843)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:180)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:175)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.os.Binder.execTransact(Binder.java:288)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at dalvik.system.NativeStart.run(Native Method)

我在谷歌上搜索了这个错误,但似乎可以得到有关此错误的任何信息。我​​查看了android源代码,问题似乎是当你没有专辑封面时生成一个 url“content://media/external/audio/albumart/-1”,并且 urlmatcher 与指定的任何 url 都不匹配,然后我抛出此错误。

URI_MATCHER.addURI("media", "*/audio/albumart", AUDIO_ALBUMART);
URI_MATCHER.addURI("media", "*/audio/albumart/#", AUDIO_ALBUMART_ID);

有人知道我该如何解决这个问题吗?

I wrote a piece of code to add a ringtone from a URL in Android 2.1. In Froyo it does not want to work at all.

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
.fromFile(file)));

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA,
file.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, filenameBase);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");         
values.put(AudioColumns.IS_RINGTONE, true);
values.put(AudioColumns.IS_NOTIFICATION, false);
values.put(AudioColumns.IS_ALARM, false);
values.put(AudioColumns.IS_MUSIC, false);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(file
.getAbsolutePath());

Uri newUri = RingtoneModule.this.getContentResolver()
.insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
RingtoneModule.this, RingtoneManager.TYPE_RINGTONE,
newUri);

When I get to insert I get this exception:

11-17 09:54:51.802: ERROR/DatabaseUtils(379): java.lang.IllegalStateException: Unknown URL: content://media/external/audio/albumart/-1
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.query(MediaProvider.java:1666)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.getAlbumArtOutputUri(MediaProvider.java:2983)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.makeThumbInternal(MediaProvider.java:3192)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.getThumb(MediaProvider.java:3070)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:2029)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1843)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:180)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:175)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at android.os.Binder.execTransact(Binder.java:288)
11-17 09:54:51.802: ERROR/DatabaseUtils(379):     at dalvik.system.NativeStart.run(Native Method)

I google my a** of for this error but can seem to get anything info about this error.I looked at android source and the problem seems to be that when you dont have album art it generates a url "content://media/external/audio/albumart/-1" and the urlmatcher does not match any of the urls specified and then i throws this error.

URI_MATCHER.addURI("media", "*/audio/albumart", AUDIO_ALBUMART);
URI_MATCHER.addURI("media", "*/audio/albumart/#", AUDIO_ALBUMART_ID);

Does someone have any idea how I can get around that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-10-10 06:23:39

我在少数用户的应用程序中遇到了这个错误,但我最终修复了它。

在我的应用程序中,我告诉媒体扫描仪不要通过将文件放入名为 .nomedia 的目录中来扫描我的目录,我认为这可能会让媒体扫描仪感到困惑,因为我想使用目录中的文件。

因此,当设置为铃声/通知/警报被激活时,我将所需的声音复制到名为 /sdcardpath/ringtones /sdcardpath/notifications /sdcardpath/alarms 的目录,然后使用相同的代码从那里设置文件。

这是我制作的复制功能

    public void ringtonemove(String ringtype){


         String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
         String outpath = sdcard + "/ringtones";
         String path = sdcard + "/multi10/" + Global.currentboard + "/series10";

         if (ringtype == "MultiboardRing") {outpath = sdcard + "/ringtones/";}
         if (ringtype == "MultiboardNotif") {outpath = sdcard + "/notifications/";}
         if (ringtype == "MultiboardAlarm") {outpath = sdcard + "/alarms/";}

    File in = new File(path, Global.currentsound);
    File out = new File(outpath, ringtype + ".ogg");
    Global.k = outpath + ringtype + ".ogg";



         File folderR = new File(Environment.getExternalStorageDirectory() + "/ringtones");
         File folderN = new File(Environment.getExternalStorageDirectory() + "/notifications");
         File folderA = new File(Environment.getExternalStorageDirectory() + "/alarms");
         if (folderR.exists()); else {folderR.mkdir();}
         if (folderN.exists()); else {folderN.mkdir();}
         if (folderA.exists()); else {folderA.mkdir();}



                     Log.d("Notice", "Copying sound file " + in);
                     try {
                        FileInputStream fis = new FileInputStream(in);
                           int size = fis.available();
                             byte[] buffer = new byte[size];
                             fis.read(buffer);
                             fis.close();

                             FileOutputStream fos = new FileOutputStream(out);
                             fos.write(buffer);
                             fos.close();



                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


    }

然后我用以下代码设置为铃声:

 public void function1(int id){
            Toast.makeText(this, "Set as ringtone" , Toast.LENGTH_SHORT).show();
            String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();

                  String path = sdcard + "/multi10/" + Global.currentboard + "/series10";


                    ringtonemove("MultiboardRing");

                    File k = new File(Global.k);

                        ContentValues values = new ContentValues();
                        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                        values.put(MediaStore.MediaColumns.TITLE, "MultiboardRing");
                        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
                        values.put(MediaStore.Audio.Media.ARTIST, "Unknown artist");
                        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
                        values.put(MediaStore.Audio.Media.IS_ALARM, false);
                        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                        Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
                        getContentResolver().insert(uri,values);
                        getContentResolver().delete(uri,MediaStore.MediaColumns.TITLE + "=\"" + "MultiboardRing" +"\"", null);
                        Uri newUri = getContentResolver().insert(uri, values);

                        RingtoneManager.setActualDefaultRingtoneUri(
                          series10button.this,
                          RingtoneManager.TYPE_RINGTONE,
                          newUri);

        }

希望这对某人有帮助,因为我花了很长时间才解决这个问题

I had this error in my application with a handful of users, I finally fixed it though.

In my application I told the media scanner not to scan my directories by putting a file in the directory called .nomedia I think this might have been confusing the media scanner because I wanted to use a file from within the directory.

So when set as ringtone/notification/alarm was activated I copied the required sound to a directory called /sdcardpath/ringtones /sdcardpath/notifications /sdcardpath/alarms and then used the same code to set the file from there.

This is the copy function I made

    public void ringtonemove(String ringtype){


         String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
         String outpath = sdcard + "/ringtones";
         String path = sdcard + "/multi10/" + Global.currentboard + "/series10";

         if (ringtype == "MultiboardRing") {outpath = sdcard + "/ringtones/";}
         if (ringtype == "MultiboardNotif") {outpath = sdcard + "/notifications/";}
         if (ringtype == "MultiboardAlarm") {outpath = sdcard + "/alarms/";}

    File in = new File(path, Global.currentsound);
    File out = new File(outpath, ringtype + ".ogg");
    Global.k = outpath + ringtype + ".ogg";



         File folderR = new File(Environment.getExternalStorageDirectory() + "/ringtones");
         File folderN = new File(Environment.getExternalStorageDirectory() + "/notifications");
         File folderA = new File(Environment.getExternalStorageDirectory() + "/alarms");
         if (folderR.exists()); else {folderR.mkdir();}
         if (folderN.exists()); else {folderN.mkdir();}
         if (folderA.exists()); else {folderA.mkdir();}



                     Log.d("Notice", "Copying sound file " + in);
                     try {
                        FileInputStream fis = new FileInputStream(in);
                           int size = fis.available();
                             byte[] buffer = new byte[size];
                             fis.read(buffer);
                             fis.close();

                             FileOutputStream fos = new FileOutputStream(out);
                             fos.write(buffer);
                             fos.close();



                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


    }

Then I set as ringtone with this code:

 public void function1(int id){
            Toast.makeText(this, "Set as ringtone" , Toast.LENGTH_SHORT).show();
            String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();

                  String path = sdcard + "/multi10/" + Global.currentboard + "/series10";


                    ringtonemove("MultiboardRing");

                    File k = new File(Global.k);

                        ContentValues values = new ContentValues();
                        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                        values.put(MediaStore.MediaColumns.TITLE, "MultiboardRing");
                        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
                        values.put(MediaStore.Audio.Media.ARTIST, "Unknown artist");
                        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
                        values.put(MediaStore.Audio.Media.IS_ALARM, false);
                        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                        Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
                        getContentResolver().insert(uri,values);
                        getContentResolver().delete(uri,MediaStore.MediaColumns.TITLE + "=\"" + "MultiboardRing" +"\"", null);
                        Uri newUri = getContentResolver().insert(uri, values);

                        RingtoneManager.setActualDefaultRingtoneUri(
                          series10button.this,
                          RingtoneManager.TYPE_RINGTONE,
                          newUri);

        }

Hope this helps someone as it took me ages to work this one out

捎一片雪花 2024-10-10 06:23:39

您应该修改uri,不可能找到文件“-1”。如果您在调用 Intent 之前保存了文件,也许它会起作用。

You should revise the uri, it's not possible to find the file "-1". Maybe if you saved the file before calling the Intent, it would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文