2011-12-28 66 views
0

大家好我一直在尋找一段時間,一直無法找到我的問題的答案。我是Android編程的新手,並且在大學學習了一些但並不多的Java,但採用了其他語言,因此我理解了概念。我在問我如何實現我創建的數組到我的保存功能。我以爲我知道如何,但只是通過saveasRing(0)我的數組包含原始值,但我收到一個力量關閉,這就是爲什麼我問。這是我的代碼。設置數組以保存Android應用程序中的聲音

我不是要求別人寫我的代碼,而是向我解釋做什麼......我想學習,並希望我只是忽略了一些東西,但如果不是,請引導我走向正確的方向。 謝謝!

String[] name ={ "a1", "a", "b","c"}; //code shortened 
int[] sounds = {R.raw.a1, R.raw.a }; //code shortened for ease of reading 

public void function1(int id){ // when I change int to int[] Eclipse wants me 
           // to change alot of things and then the app will 
           // force close on start 
    saveasRing(sounds);   // right here I have tried passing "0" instead 
           // of sounds, a no go. 

    Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show(); 
}//Closes Function 1 

//Save as Ringtone Coding for Function 3 
public boolean saveasRing(int ressound){ 
    byte[] buffer=null; 
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
    int size=0; 

    try { 
    size = fIn.available(); 
    buffer = new byte[size]; 
    fIn.read(buffer); 
    fIn.close(); 
    } 
    catch (IOException e) { 
    // TODO Auto-generated catch block 
    return false; 
    } 

    String path="/sdcard/media/audio/ringtones/"; 
    String filename= name +".mp3"; 

    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) { 
    // TODO Auto-generated catch block 
    return false; 
    } 
    catch (IOException e) { 
    // TODO Auto-generated catch block 
    return false; 
    }  

    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, "dosequis"); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
    values.put(MediaStore.Audio.Media.ARTIST, "Most Interesting Man "); 
    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); 

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

    return true; 
} 
//Closes Save as Ringtone 

這裏是當我長命中鈴聲會發生什麼Logcat。上下文菜單很好,只是當我選擇要保存的類型時,我得到一個關閉的力量和這些錯誤。再次感謝您的時間。

請讓我知道是否有更好的方式來格式化。

W/ResourceType(225):No package identifier when getting name for resource number 0x00000000 

D/AndroidRuntime(225):Shutting down VM 

W/dalvikvm(225): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 

E/AndroidRuntime(225):Uncaught handler: thread main exiting due to uncaught exception 

E/AndroidRuntime(225):android.content.res.Resources$NotFoundException: Resource ID #0x0 

E/AndroidRuntime(225):at android.content.res.Resources.getValue(Resources.java:891) 

E/AndroidRuntime(225):at Android.content.res.Resources.openRawResource(Resources.java:816) 

E/AndroidRuntime(225):at android.content.res.Resources.openRawResource(Resources.java:798) 

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.saveasRing(DosEquisSoundboardActivity.java:270) 

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.function1(DosEquisSoundboardActivity.java:253) 

E/AndroidRuntime(225):at tomcavell.app.DosEquisSoundboardActivity.onContextItemSelected(DosEquisSoundboardActivity.java:245) 

E/AndroidRuntime(225):at android.app.Activity.onMenuItemSelected(Activity.java:2174) 

E/AndroidRuntime(225):at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2731) 

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139) 

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 

E/AndroidRuntime(225):at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:129) 

E/AndroidRuntime(225):at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884) 

E/AndroidRuntime(225):at android.widget.AdapterView.performItemClick(AdapterView.java:284) 

E/AndroidRuntime(225):at android.widget.ListView.performItemClick(ListView.java:3285) 

E/AndroidRuntime(225):at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640) 

E/AndroidRuntime(225):at android.os.Handler.handleCallback(Handler.java:587) 

E/AndroidRuntime(225):at android.os.Handler.dispatchMessage(Handler.java:92) 

E/AndroidRuntime(225):at android.os.Looper.loop(Looper.java:123) 

E/AndroidRuntime(225):at android.app.ActivityThread.main(ActivityThread.java:4363) 

E/AndroidRuntime(225):at java.lang.reflect.Method.invokeNative(Native Method) 

E/AndroidRuntime(225):at java.lang.reflect.Method.invoke(Method.java:521) 

E/AndroidRuntime(225):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 

E/AndroidRuntime(225):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

E/AndroidRuntime(225):at dalvik.system.NativeStart.main(Native Method) 

I/dalvikvm(225): threadid=7: reacting to signal 3 

I/dalvikvm(225): Wrote stack trace to '/data/anr/traces.txt' 
+1

您將需要包含您在logcat下獲得的任何異常,以便我們真正知道問題出在哪裏。我看到有關讀寫IO流可能導致其他問題的幾個問題,但爲什麼它的部隊關閉並不明顯。 – chubbsondubs

+0

歡迎來到StackOverflow。請花時間正確地設置代碼的格式,並使用空格縮進而不是製表符。 (提示:您可以在您輸入問題的區域下面實時預覽帖子的格式。)正確格式化可以讓人們更容易閱讀,提高獲得答案的機會,並且避免其他人不得不爲你編輯它。 :) 謝謝。 –

+0

謝謝Ken,就像我說過的,我對Java很陌生並且對它進行格式化。現在,我會讓它看起來像現在一樣!謝謝。 – Cavell219

回答

0

我想通了,我的問題。我不得不設置代碼與一個int然後設置INT爲

j=sounds[i]; //(assuming j was the int declared) 

然後通過功能與我saveasRing在

saveasRing[i]; 

感謝您尋找和幫助!