Tengo una lista de sonidos la cual al dejar pulsado sobre los botones sale un "context menu" donde decidir si se comparte el sonido, se usa como tono o como notificacion, tengo el codigo hecho y guarda correctamente el sonido como tono o notificacion, pero el problema es que me guarda el mismo sonido en todos los botones, eso es porque uso "R.raw.misonido" (para hacer la prueba) en la funcion que tengo definida, y la pregunta es, como puedo hacer para que me guarde el sonido correspondiente al boton que se pulse?... quizas con arrays? os agradezco cualquier ayuda.
Este es mi codigo:
1- Registro los botones para el context menu
registerForContextMenu(btn_sonido);
registerForContextMenu(btn_sonido2);
registerForContextMenu(btn_sonido3);
... i asi hasta 30 botones
2 - Defino el "onCreateContextMenu" y el "onContextItemSelected"
3 - Funcion para usar sonido como notificacion:
public void function2(int id){
// Guardar como notificacion
if (savenot(
R.raw.misonido)) {
Toast.makeText(this, "Guardado como Notificacion", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Fallo, Revisa la SD", Toast.LENGTH_SHORT).show();
}
4 - La funcion sigue para guardar el sonido.
(Click para mostrar/ocultar)public boolean savenot(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(re ssound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String path="/sdcard/media/audio/notifications/";
String filename="Titulo del Sonido"+".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, "Titulo del Sonido");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Artista");
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);
this.getContentResolver().insert(
MediaStore.Audio.Media.getContentUriForPath(k.getA bsolutePath()), values);
return true;
}