我创建了一个onClick播放mp3文件的按钮。我还创建了一个上下文菜单,当你按下按钮2秒时,它会提示你将其保存为铃声。如何将它保存在我的sd中?这是我的代码:
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Toast.makeText(a.this, "Touch and listen", Toast.LENGTH_SHORT).show();
Button button = (Button) findViewById(R.id.btn1);
registerForContextMenu(button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
MediaPlayer mp = MediaPlayer.create(a.this, R.raw.myalo);
mp.start();
Toast.makeText(a.this, "Eisai sto myalo", Toast.LENGTH_SHORT).show();
}
});
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save As:");
menu.add(0, v.getId(), 0, "Ringtone");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone Saved", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
要获取SD卡上的“公共”目录以保存铃声和创建输出流,请使用...
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES);
path.mkdirs(); // Ensure the directory exists
File file = new File(path, "MyRingtone.mp3");
OutputStream os = new FileOutputStream(file);
然后将文件保存到输出流。