图像在Game Maker Studio中使用Java扩展选择Android

时间:2016-08-22 17:02:06

标签: java android game-maker

我正在尝试为Game Maker Studio Android App制作Java扩展程序。

我曾尝试使用在线教程中的代码,但GMS使用了不同的方法,但我无法使用它。

我正在使用以下代码:

package ${YYAndroidPackageName};

//Basic imports
import android.util.Log;
import java.lang.String;
import java.lang.String;

import android.app.Activity;
import android.content.Intent;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

//Import Game Maker classes
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.RunnerActivity;

public class pickimage extends Activity {

    public static final int PICK_IMAGE = 1;
    private static final int EVENT_OTHER_SOCIAL = 70;

    public void selectimage() 
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
        int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
        RunnerJNILib.DsMapAddString( dsMapIndex, "type", "pickeropened" );
        RunnerJNILib.DsMapAddDouble( dsMapIndex, "argument0", 2);
        RunnerJNILib.DsMapAddDouble( dsMapIndex, "argument0", 3);
        RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        URI selectedImageUri = null;
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedImageUri = new URI(data.getData());
                //String selectedImagePath = getPath(selectedImageUri);
                //Log.i("yoyo", selectedImagePath);
                //return selectedImagePath;
    int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null,null);
    RunnerJNILib.DsMapAddString( dsMapIndex, "type", "imagepath" );
    RunnerJNILib.DsMapAddString( dsMapIndex, "path", selectedImagePath);
    RunnerJNILib.DsMapAddString( dsMapIndex, "path", "boo");
    RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
            }
        }
    }

    /**
     * helper to retrieve the path of an image URI

    public String getPath(Uri uri) {
            // just some safety built in 
            if( uri == null ) {
                // TODO perform some logging or show user feedback
                return null;
            }
            // try to retrieve the image from the media store first
            // this will only work for images selected from gallery
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if( cursor != null ){
                int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }
            // this is our fallback here
            return uri.getPath();
    }*/

}

我已经按照GMS扩展教程进行操作了。以下是控制台的一些错误通知:

:com.companyname.Test_Image_Chooser:compileReleaseJavaWithJavacF:\GM\Cache\Test_Image_Chooser\Android\Default\com.companyname.Test_Image_Chooser\src\main\java\com\companyname\Test_Image_Chooser\pickimage.java:41: error: cannot find symbol
            if (requestCode == SELECT_PICTURE) {
                               ^
  symbol:   variable SELECT_PICTURE
  location: class pickimage
F:\GM\Cache\Test_Image_Chooser\Android\Default\com.companyname.Test_Image_Chooser\src\main\java\com\companyname\Test_Image_Chooser\pickimage.java:42: error: incompatible types: Uri cannot be converted to String
                selectedImageUri = new URI(data.getData());
                                                       ^
F:\GM\Cache\Test_Image_Chooser\Android\Default\com.companyname.Test_Image_Chooser\src\main\java\com\companyname\Test_Image_Chooser\pickimage.java:48: error: cannot find symbol
    RunnerJNILib.DsMapAddString( dsMapIndex, "path", selectedImagePath);
                                                     ^
  symbol:   variable selectedImagePath
  location: class pickimage
Note: Some input files use or override a deprecated API.
* What went wrong:
Execution failed for task ':com.companyname.Test_Image_Chooser:compileReleaseJavaWithJavac'.

我想要实现的是用户在app中按下按钮,它会打开Native Image Picker对话框,用户选择一个文件。然后用户按下另一个按钮,应用程序获取所选图像的完整路径,然后将其上传到我的服务器。

我是Java新手:)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我在你的java文件中看到一些问题,当你使用意图时,你必须通知jnirunner你正在激活意图,并且你还有一个额外的不需要的参数和其他额外的东西,所以这段代码

startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
    RunnerJNILib.DsMapAddString( dsMapIndex, "type", "pickeropened" );
    RunnerJNILib.DsMapAddDouble( dsMapIndex, "argument0", 2);
    RunnerJNILib.DsMapAddDouble( dsMapIndex, "argument0", 3);
    RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);

应该看起来更像这样,并在它之前有一个连接器声明来通知跑步者,并且此时不要调用异步事件:

RunnerActivity.CurrentActivity.startActivityForResult(intent, PICK_IMAGE);

所以在你的onActivityResult代码中你已经注释掉了代码的重要部分,所以async事件没有得到它需要的信息,并且那里有一些额外的东西和"路径& #34;你用不同的输出变量说了两次的代码行,所以这个

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    URI selectedImageUri = null;
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            selectedImageUri = new URI(data.getData());
            //String selectedImagePath = getPath(selectedImageUri);
            //Log.i("yoyo", selectedImagePath);
            //return selectedImagePath;
            int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null,null);
            RunnerJNILib.DsMapAddString( dsMapIndex, "type", "imagepath" );
            RunnerJNILib.DsMapAddString( dsMapIndex, "path", selectedImagePath);
            RunnerJNILib.DsMapAddString( dsMapIndex, "path", "boo");
            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
        }
    }
}

应该成为这个

public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(int requestCode, int resultCode, Intent data)
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_IMAGE) {
            String path = data.getData().toString());
            Log.i("yoyo", path);
            int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null,null);
            RunnerJNILib.DsMapAddString( dsMapIndex, "type", "imagepath" );
            RunnerJNILib.DsMapAddString(result, "selectedImageUri", path);
            RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
        }
    }
}