gpt4 book ai didi

android - 设置数组以在Android应用中保存声音

转载 作者:行者123 更新时间:2023-12-03 02:23:27 24 4
gpt4 key购买 nike

大家好,我已经看了一段时间了,一直找不到我的问题的答案。我是Android编程的新手,在大学学习过Java,但是学习了很多,但是学习了其他语言,所以我理解了一些概念。我在问如何实现在保存函数中创建的数组。我以为我知道,但是只传递了saveasRing(0),其中我的数组包含原始值,但是我收到一个强制关闭,所以这就是我要问的原因。这是我的代码。

我不是要别人写我的代码,而是要向我解释该怎么做...我想学习,希望我只是忽略了一些东西,如果不是,请引导我朝正确的方向发展。
谢谢!

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'

最佳答案

我想出了我的问题。我必须使用int设置代码,然后将该int设置为

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

然后用i传递函数saveasRing
saveasRing[i];

感谢您的帮助!

关于android - 设置数组以在Android应用中保存声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com