2014-10-21 83 views
0

因此,我正在製作一個soundboard應用程序,並試圖將通知保存爲聲音。然而,問題在於,只要用戶確認存儲爲通知,應用程序就會崩潰。保存聲音作爲通知崩潰的應用程序

繼承人是我迄今爲止:當它崩潰

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; 
} 

這裏的logcat的輸出:精細

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) 

對話框彈出,但只要我點擊「另存爲通知「,該應用程序崩潰。

+0

我認爲當您嘗試創建'InputStream'時會發生問題,因爲沒有該ID的資源。 – Titus 2014-10-21 22:24:59

+0

@Titus請你詳細說明一下嗎?當談到這個東西時,我是一個完整的初學者,所以你能解釋一下如何解決這個問題或者爲什麼它不工作? – Shalashaska 2014-10-22 19:44:35

回答

0

我認爲,這部分代碼導致錯誤 InputStream fIn = getActivity().getBaseContext().getResources().openRawResource(sound.getSoundResourceId());

錯誤消息說你正試圖與ID -1打開資源,確保getSoundResourceId()返回正確的ID號。

相關問題