Set Sound as ringtone with long press and context menu?
<p>I'm trying to set sound as ringtone when pressing long click 
where my app play sound when user click one click ,
I saved sounds as Integer Array for their positions on Raw file 
and I used the following code that shows context menu when press long click then user can choose what action it needs , I'm stuck where my code should get the specific sound from clicked item on gridview and set it as ringtone
my code is :</p>

<pre><code>package com.De7kat.Grid;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class De7katGrid extends Activity {
private MediaPlayer mp;

public Integer[] mSongsIds = { R.raw.l_1, R.raw.l_2, R.raw.l_3, R.raw.l_4,
        R.raw.l_5, R.raw.l_6, R.raw.l_7, R.raw.l_8, R.raw.tom, R.raw.love,
        R.raw.b_1, R.raw.b_2, R.raw.b_3, R.raw.b_4, };

public Object getItem(int position) {
    return mSongsIds[position];

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);
    registerForContextMenu(gridView);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View v,
                int position, long id) {

            stopPlaying();
            mp = MediaPlayer.create(De7katGrid.this, mSongsIds[position]);
            mp.start();

        }
    });
}

@Override
public void onPause() {
    super.onPause();
    stopPlaying();

}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;

    }
}

    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)         {  
    super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "Set As RingTone");  
        menu.add(0, v.getId(), 0, "Set As Notification");


    }  


    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="Set As RingTone"){
            function1(item.getItemId());
            }  
        else if(item.getTitle()=="Set As Notification"){function2(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    public  void function1(int id){ 

        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
        // &gt;&gt;&gt;  savering(mSongsIds[position]); &lt;&lt;&lt;&lt; Here I want to get sound as above where I can play sound by getting it's position from mSongsIds array

    }


    public void function2(int id){  
        Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();  
    } 


    public boolean savering(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="soundtest4"+".ogg";  

           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, "HahaSound");  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
           values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
           values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
           values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);  
           values.put(MediaStore.Audio.Media.IS_ALARM, true);  
           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;  
          }  

}
</code></pre>
