简体   繁体   中英

Saving sound as notification crashes app

So I'm making a soundboard app and I'm trying to implement saving sounds as notifications. The problem, however, is that the app crashes as soon as the user confirms saving as notification.

Heres what I have so far:

private OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v,
            final int position, long id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        CharSequence[] menu = new CharSequence[1];
        menu[0] = "Notification";
        // menu[1] = "Ringtone";

        builder.setTitle("Save As...").setItems(menu,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                        switch (which) {
                        case 0:
                            if (saveAsNotification(getSounds()
                                    .get(position))) {
                                Toast.makeText(getActivity(),
                                        "Saved as Notification",
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getActivity(),
                                        "Failed to Save Notification",
                                        Toast.LENGTH_SHORT).show();
                            }
                            break;
                        default:
                            break;
                        }
                    }
                });
        builder.create().show();
        return true;
    }
};

public boolean saveAsNotification(Sound sound) {
    byte[] buffer = null;
    InputStream fIn = getActivity().getBaseContext().getResources()
            .openRawResource(sound.getSoundResourceId());
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    String path = Environment.getExternalStorageDirectory().getPath()
            + "/media/audio/notifications/";
    String filename = sound.getDescription() + ".ogg";

    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    getActivity().sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
                    .parse("file://" + path + filename)));

    File k = new File(path, filename);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, sound.getDescription());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
    values.put(MediaStore.Audio.Media.ARTIST, "soundboarder");
    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);

    // Insert it into the database
    getActivity().getContentResolver()
            .insert(MediaStore.Audio.Media.getContentUriForPath(k
                    .getAbsolutePath()), values);

    return true;
}

Here's the logcat output when it crashes:

10-21 18:56:54.315: W/ResourceType(19905): No known package when getting value for resource number 0xffffffff
10-21 18:56:54.315: D/AndroidRuntime(19905): Shutting down VM
10-21 18:56:54.315: W/dalvikvm(19905): threadid=1: thread exiting with uncaught exception (group=0x41510ba8)
10-21 18:56:54.315: E/AndroidRuntime(19905): FATAL EXCEPTION: main
10-21 18:56:54.315: E/AndroidRuntime(19905): Process: com.metrico.trailerparkboyssoundboard, PID: 19905
10-21 18:56:54.315: E/AndroidRuntime(19905): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.content.res.Resources.getValue(Resources.java:1123)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.content.res.Resources.openRawResource(Resources.java:1038)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.content.res.Resources.openRawResource(Resources.java:1015)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at com.metrico.trailerparkboyssoundboard.fragments.SoundboardFragment.saveAsNotification(SoundboardFragment.java:130)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at com.metrico.trailerparkboyssoundboard.fragments.SoundboardFragment$1$1.onClick(SoundboardFragment.java:106)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.widget.AbsListView$3.run(AbsListView.java:3638)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.os.Handler.handleCallback(Handler.java:733)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.os.Looper.loop(Looper.java:136)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at android.app.ActivityThread.main(ActivityThread.java:5017)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at java.lang.reflect.Method.invokeNative(Native Method)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at java.lang.reflect.Method.invoke(Method.java:515)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-21 18:56:54.315: E/AndroidRuntime(19905):    at dalvik.system.NativeStart.main(Native Method)

The dialogue box pops of fine, but as soon as I click on "save as notification", the app crashes.

I think that this part of the code causes the error InputStream fIn = getActivity().getBaseContext().getResources().openRawResource(sound.getSoundResourceId());

The error message says you are trying to open the resource with the ID -1 , make sure getSoundResourceId() returns the correct ID number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM