Save as Ringtone from ContextMenu
I have created a button that onClick plays a mp3 file.I have also create a context menu that when you press the button for 2 secs it prompts you to save it as ringtone.How can i save it somewhere in my sd?this is my code:
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 on开发者_如何学运维ContextItemSelected(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();
}
}
To get the 'public' directory on the SD card for saving ringtones and to create an output stream, use...
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);
Then save your file to the output stream.
精彩评论