Can't get onActivityResult to work..

Search This thread

Lodmot

New member
Aug 24, 2010
2
0
Hi everybody. ^^

I'm in the process of making an app using Game Maker Studio, and I'm trying to code an Android extension for it in Java.
Basically what I want the extension to do is start a Gallery Image picker activity, where a user can select a picture from their phone, then the selected image would be inserted into the game.

I have it so that my extension can bring up the image picker activity, but that's as far as it gets before it clonks out. ><
Upon further testing, I was able to determine that where it gets stuck is right after the startActivityForResult command. The onActivityResult method is never initiated, so the extension isn't doing anything else from there. If someone could give me an idea why this isn't working, that would be awesome. Thanks! ^^

Here is the code for my extension. Also note that the (RunnerActivity.CurrentActivity) is basically referring to the activity where the game is displayed.

Code:
package ${YYAndroidPackageName};
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import ${YYAndroidPackageName}.R;
import ${YYAndroidPackageName}.RunnerActivity;
import com.yoyogames.runner.RunnerJNILib;

public class GalleryChooser extends Activity
{

String imagePath = "";
Intent i;

private static final int RESULT_LOAD_IMAGE=1;

public void ImageSelector() {
                 
                i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                (RunnerActivity.CurrentActivity).startActivityForResult(i, RESULT_LOAD_IMAGE);
            }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
		super.onActivityResult(requestCode, resultCode, data);
//      if (requestCode == RESULT_LOAD_IMAGE && resultCode == (RunnerActivity.CurrentActivity).RESULT_OK && null != data) {
            if (requestCode == RESULT_LOAD_IMAGE && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = (RunnerActivity.CurrentActivity).getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imagePath = cursor.getString(columnIndex);
		 
	       cursor.close();
		 
	
	 }
	else {imagePath = "Blank location returned :(";}
 }
 public String getImagePath() {
	return imagePath;}
}
 
Last edited:

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Hi everybody. ^^

I'm in the process of making an app using Game Maker Studio, and I'm trying to code an Android extension for it in Java.
Basically what I want the extension to do is start a Gallery Image picker activity, where a user can select a picture from their phone, then the selected image would be inserted into the game.

I have it so that my extension can bring up the image picker activity, but that's as far as it gets before it clonks out. ><
Upon further testing, I was able to determine that where it gets stuck is right after the startActivityForResult command. The onActivityResult method is never initiated, so the extension isn't doing anything else from there. If someone could give me an idea why this isn't working, that would be awesome. Thanks! ^^

Here is the code for my extension. Also note that the (RunnerActivity.CurrentActivity) is basically referring to the activity where the game is displayed.

Code:
package ${YYAndroidPackageName};
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import ${YYAndroidPackageName}.R;
import ${YYAndroidPackageName}.RunnerActivity;
import com.yoyogames.runner.RunnerJNILib;

public class GalleryChooser extends Activity
{

String imagePath = "";
Intent i;

private static final int RESULT_LOAD_IMAGE=1;

public void ImageSelector() {
                 
                i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                (RunnerActivity.CurrentActivity).startActivityForResult(i, RESULT_LOAD_IMAGE);
            }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
		super.onActivityResult(requestCode, resultCode, data);
//      if (requestCode == RESULT_LOAD_IMAGE && resultCode == (RunnerActivity.CurrentActivity).RESULT_OK && null != data) {
            if (requestCode == RESULT_LOAD_IMAGE && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = (RunnerActivity.CurrentActivity).getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imagePath = cursor.getString(columnIndex);
		 
	       cursor.close();
		 
	
	 }
	else {imagePath = "Blank location returned :(";}
 }
 public String getImagePath() {
	return imagePath;}
}

In your onActivityResult(), in the line which you commented out to be replaced by the fewer conditions you should be checking the requestCode instead of the resultCode! Read the doc about activity results again, you pass the requestCode to startActivityForResult() and the resultCode is usually either RESULT_OK or RESULT_CANCELED! Best to check both codes like they do in the example I linked to above.
 

Lodmot

New member
Aug 24, 2010
2
0
In your onActivityResult().........

Well the reason why that line is commented out is because I actually tried doing it like this:

Code:
      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

... but doing that gave me an error when I compiled it. It said "can't find symbol RESULT_OK" or something similar.
EDIT: NOW apparently it's compiling fine with the above! It must've had to do with the SDK Manager getting a new API level or something... However it's still not working the way I want it to...

The only way I was able to get that line to compile without errors was by doing it this way:

Code:
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == (RunnerActivity.CurrentActivity).RESULT_OK && null != data) {

However I have a feeling the RESULT_OK variable doesn't work that way, because it's not allowing that method to initiate when it's like that..

EDIT: I took the time to read a bit deeper into it, and it LOOKS like I might want to use setResult(). I'll try it when I get back to my computer.
EDIT 2: Nope, no workie. :/

EDIT 3: I'm able to compile the code exactly the way as it's described in the example you directed me to, but it's not performing the onActivityResult method still.. I tried using setResult(), that didn't work... A clever trick I'm trying is having a string variable set to different messages after each individual command is ran. Then I'm drawing that string variable in the game itself. Wherever the string variable stops changing will identify where the code error is, and it's definitely stopping right before the onActivityResult method begins. However, that could also happen if the game gets paused when it opens up Gallery (but even so, it should update once the game activity regains focus I would think)... Man... what a mystery! >.<

Just to keep my progress up to date, this is how my code looks now, and it compiles correctly:

Code:
package ${YYAndroidPackageName};
 
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import ${YYAndroidPackageName}.R;
import ${YYAndroidPackageName}.RunnerActivity;
import com.yoyogames.runner.RunnerJNILib;

public class GalleryChooser extends Activity
{

String imagePath = "";
Intent i;

private static final int RESULT_LOAD_IMAGE=1;

public void ImageSelector() {
                 
                i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 imagePath="activity started!";
                (RunnerActivity.CurrentActivity).startActivityForResult(i, RESULT_LOAD_IMAGE);
			imagePath="Activity Started 2!!! (we're still running in the background)";
            }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         
		imagePath="hello world...?";
		//super.onActivityResult(requestCode, resultCode, data);
		imagePath="checking request code...";
      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
		imagePath="RESULT_LOAD_IMAGE verified!";
            Uri selectedImage = data.getData();
		imagePath="Grabbing data...";
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 		imagePath="Converting String....";
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
		imagePath="Got Content Resolver!";
            cursor.moveToFirst();
 		imagePath="Moved to First...";
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imagePath = cursor.getString(columnIndex);
		 
	       cursor.close();
	 }
	else {imagePath = "Blank location returned :(";}
 }
 public String getImagePath() {
	return imagePath;}
}
 
Last edited: