0

My code refused to return the right requestCode so I've improvised a bit. It always returned -1.

    if(v.getId() == R.id.imageButton9)
    {
        request = 888;
        Intent wpIntent = new Intent();
        wpIntent.setType("image/*");
        wpIntent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"), SELECT_PICTURE);
    }

    else if(v.getId() == R.id.imageButton10)
    {
        String uri = null;
        request = 999;
        rtIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
        if( uri != null)
        {
            rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(uri));
        }
        else
        {
            rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
        }       
        this.startActivityForResult(rtIntent, SELECT_TONE);
    }

I've declared two variables at the beginning of my code:

    protected static final int SELECT_PICTURE = 888;
protected static final int SELECT_TONE = 999;

Here's the onActivityResult:

    public void onActivityResult(int resultCode, int requestCode, Intent data)
{
    Toast.makeText(this, "onActivityResult: "+requestCode, Toast.LENGTH_SHORT).show();

    if(request == 888)
    {
        Toast.makeText(this, "Picture Selection", Toast.LENGTH_SHORT).show();
        Uri selectedImage = Uri.parse(data.getDataString());
        wpPath = getPath(selectedImage);
        Toast.makeText(this, "WP Path: "+wpPath, Toast.LENGTH_SHORT).show();
        request = 0;
    }

    else if(request == 999)
    {
        Toast.makeText(this, "Tone Selection", Toast.LENGTH_SHORT).show();
        Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());
        Uri uri = rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        if (uri != null) 
        {
            rtPath = uri.toString();
            Toast.makeText(this, "Ringtone Path: "+rtPath, Toast.LENGTH_SHORT).show();
            request = 0;
        }
    }
}

This always force closes. It give the following error:

06-01 12:40:35.388: E/AndroidRuntime(7437): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=999, result=-1, data=Intent { (has extras) }} to activity {com.asim.autobot/com.asim.autobot.profile}: java.lang.NullPointerException

I'm stuck. Can't solve this problem.

First of all, why does the requestCode always return -1? Secondly, why does the application force close on rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);?

EDIT: Full Logcat http://i47.tinypic.com/1zm2gww.jpg

The Line it points to is the Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());

Asim
  • 6,962
  • 8
  • 38
  • 61
  • Use switch case instead of checking the button with if (if(v.getId() == R.id.imageButton9)) – Aerrow Jun 01 '12 at 08:20

2 Answers2

2

When you get a resultCode of -1 then it worked as RESULT_OK = -1

Checking the following line:

rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString()

You have a parameter called Intent data and you should work on that instead of your rtIntent variable. Your rtIntent variable will not be modified so getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) will probably return null.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • I love you man, in a very non-gay way. Thanks a LOT for helping. I've marked your answer as Accepted. Kindly check this question again because I might need a little help with setting the returned Uri as a ringtone. Wish I could contact you outside of stackoverflow for help :/ – Asim Jun 01 '12 at 09:52
  • Searching works, too :) Try this solution: http://stackoverflow.com/questions/7671637/how-to-set-ringtone-with-ringtonemanager-action-ringtone-picker – WarrenFaith Jun 01 '12 at 10:51
  • I saw that... See my code was kinda OK. Just that I had used the wrong intent lol – Asim Jun 02 '12 at 04:49
0

Try this code,

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.imageButton9:
            request = 888;
            Intent wpIntent = new Intent();
            wpIntent.setType("image/*");
            wpIntent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(wpIntent, "Select Picture"), SELECT_PICTURE);
        case R.id.imageButton10:

            String uri = null;
            request = 999;
            rtIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
            rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
            if( uri != null)
                {
                    rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(uri));
                }
            else
                {
                    rtIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
                }       
                        this.startActivityForResult(rtIntent, SELECT_TONE);

            break;
        default:
            break;
        }

    }


       public void onActivityResult(int resultCode, int requestCode, Intent data)
{
    Toast.makeText(this, "onActivityResult: "+requestCode, Toast.LENGTH_SHORT).show();

    if(requestCode == SELECT_PICTURE)
    {
        Toast.makeText(this, "Picture Selection", Toast.LENGTH_SHORT).show();
        Uri selectedImage = Uri.parse(data.getDataString());
        wpPath = getPath(selectedImage);
        Toast.makeText(this, "WP Path: "+wpPath, Toast.LENGTH_SHORT).show();
        request = 0;
    }

    else if(requestCode == SELECT_TONE)
    {
        Toast.makeText(this, "Tone Selection", Toast.LENGTH_SHORT).show();
        Log.i("RT Picked is: ", rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString());
        Uri uri = rtIntent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        if (uri != null) 
        {
            rtPath = uri.toString();
            Toast.makeText(this, "Ringtone Path: "+rtPath, Toast.LENGTH_SHORT).show();
            request = 0;
        }
    }
}
Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • Tried. Not working. When I click Select Ringtone, activityresult returns -1 and hence nothing. When I click Select Wallpaper, it first opens the RingtoneChooser (whether to use Android System or GoSMS). When I press back, it then opens the WallpaperChooser (Gallery, Root Explorer etc). – Asim Jun 01 '12 at 09:26