I can not get the ContextMenuResource to return the correct information that I am looking for. At times it returns what I want, other times not so much so.
public class montypython extends Activity implements OnClickListener { private static final int SAVE_SOUND = 0;
// Tag to easily locate errors in the log file.
private static final String TAG = "Monty";
// Declares the buttons.
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button button11;
Button button12;
Button button13;
Button button14;
Button button15;
Button button16;
Button button17;
Button button18;
Button button19;
Button button20;
Button button21;
Button button22;
Button button23;
Button button24;
Button button25;
// Makes contextMenuResource available for use in other methods.
public int contextMenuResource;
// Makes MediaPlayer (mp) null and ready to go.
MediaPlayer mp=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set Views and Listeners and Tags for buttons.
button0=(Button)findViewById(R.id.button0);
button0.setOnClickListener(this);
button0.setTag(R.raw.a);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
button1.setTag(R.raw.b);
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
button2.setTag(R.raw.c);
button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
button3.setTag(R.raw.d);
button4=(Button)findViewById(R.id.button4);
button4.setOnClickListener(this);
button4.setTag(R.raw.e);
button5=(Button)findViewById(R.id.button5);
button5.setOnClickListener(this);
button5.setTag(R.raw.f);
button6=(Button)findViewById(R.id.button6);
button6.setOnClickListener(this);
button6.setTag(R.raw.g);
button7=(Button)findViewById(R.id.button7);
button7.setOnClickListener(this);
button7.setTag(R.raw.h);
button8=(Button)findViewById(R.id.button8);
button8.setOnClickListener(this);
button8.setTag(R.raw.i);
button9=(Button)findViewById(R.id.button9);
button9.setOnClickListener(this);
button9.setTag(R.raw.j);
button10=(Button)findViewById(R.id.button10);
button10.setOnClickListener(this);
button10.setTag(R.raw.k);
button11=(Button)findViewById(R.id.button11);
button11.setOnClickListener(this);
button11.setTag(R.raw.l);
button12=(Button)findViewById(R.id.button12);
button12.setOnClickListener(this);
button12.setTag(R.raw.m);
button13=(Button)findViewById(R.id.button13);
button13.setOnClickListener(this);
button13.setTag(R.raw.n);
button14=(Button)findViewById(R.id.button14);
button14.setOnClickListener(this);
button14.setTag(R.raw.o);
button15=(Button)findViewById(R.id.button15);
button15.setOnClickListener(this);
button15.setTag(R.raw.p);
button16=(Button)findViewById(R.id.button16);
button16.setOnClickListener(this);
button16.setTag(R.raw.q);
button17=(Button)findViewById(R.id.button17);
button17.setOnClickListener(this);
button17.setTag(R.raw.r);
button18=(Button)findViewById(R.id.button18);
button18.setOnClickListener(this);
button18.setTag(R.raw.s);
button19=(Button)findViewById(R.id.button19);
button19.setOnClickListener(this);
button19.setTag(R.raw.t);
button20=(Button)findViewById(R.id.button20);
button20.setOnClickListener(this);
button20.setTag(R.raw.u);
button21=(Button)findViewById(R.id.button21);
button21.setOnClickListener(this);
button21.setTag(R.raw.v);
button22=(Button)findViewById(R.id.button22);
button22.setOnClickListener(this);
button22.setTag(R.raw.w);
button23=(Button)findViewById(R.id.button23);
button23.setOnClickListener(this);
button23.setTag(R.raw.x);
button24=(Button)findViewById(R.id.button24);
button24.setOnClickListener(this);
button24.setTag(R.raw.y);
button25=(Button)findViewById(R.id.button25);
button25.setOnClickListener(this);
button25.setTag(R.raw.z);
// Registering ContextMenu's for long clicks.
registerForContextMenu(button0);
registerForContextMenu(button1);
registerForContextMenu(button2);
registerForContextMenu(button3);
registerForContextMenu(button4);
registerForContextMenu(button5);
registerForContextMenu(button6);
registerForContextMenu(button7);
registerForContextMenu(button8);
registerForContextMenu(button9);
registerForContextMenu(button10);
registerForContextMenu(button11);
registerForContextMenu(button12);
registerForContextMenu(button13);
registerForContextMenu(button14);
registerForContextMenu(button15);
registerForContextMenu(button16);
registerForContextMenu(button17);
registerForContextMenu(button18);
registerForContextMenu(button19);
registerForContextMenu(button20);
registerForContextMenu(button21);
registerForContextMenu(button22);
registerForContextMenu(button23);
registerForContextMenu(button24);
registerForContextMenu(button25);
}
// Using the Tag to allow for simple media playing coding.
@Override
public void onClick(View vv) {
if (mp !=null) {mp.stop();}
mp=MediaPlayer.create(this, ((Integer)vv.getTag()).intValue());
mp.start();
}
// Create ContextMenu's.
@Override
public void onCreateContextMenu(ContextMenu menu, View vv, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, vv, menuInfo);
contextMenuResource = ((((Integer) (vv.getTag())).intValue()));
menu.setHeaderTitle(((Button)vv).getText());
// contextMenuResource used here just to see what value it contains
// when called it shows /res/raw/*.mp3 depending on which button
menu.add(0, SAVE_SOUND, 0, contextMenuResource);
}
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case SAVE_SOUND:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// contextMenuResource used here just to see what value it contains
// when called it shows at least a seven digit integer (which makes sense to me)
dialogBuilder.setMessage("You selected " + contextMenuResource);
dialogBuilder.setCancelable(true);
dialogBuilder.create().show();
// Creates a directory on the SDCard
File sddir = new File("/sdcard/NetApex/Ringtones");
if (!sddir.mkdirs()) {
Log.e(TAG, "Create dir on sdcard failed");
return false;
}
// The rest is commented out since I can't get the data I would like in order
// to copy the file to the SDCard
/* InputStream fIn = getBaseContext().getResources().openRawResource(contextMenuResource);
int size = 0;
size = fIn.available();
byte[] buffer = new byte[size];
fIn.read(buffer);
fIn.close();
File rtNew = new File("/sdcard/NetApex/Ringtones"
+ contextMenuResource
+".mp3");
FileOutputStream rtFOS = null;
rtFOS = new FileOutputStream(rtNew);
rtFOS.write(buffer);
rtFOS.flush();
rtFOS.close(); */
return true;
}
return false;
}
// Options menu created just to see it created
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
// This makes the selection work
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.quit:
finish();
return true;
}
return false;
}
}