c# - 如何从c#代码中调用java中的方法?

标签 c# java android integration

我在android(java)中编写了一个方法,即用于将图像上传到服务器。我希望从 C# 代码中调用此方法。我不知道该怎么做,java代码如下?

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     Button buttonUpload = (Button) findViewById(R.id.buttonUpload);

     buttonUpload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("imageName", imageName );
            request.addProperty("base64String", compressedImageString);
            //request.addProperty("compressedImageBitmap", compressedImageBitmap);
            SoapSerializationEnvelope envelope = new
                    SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            try {

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject result = (SoapObject)envelope.bodyIn;

                if(result != null)
                {

                    Toast.makeText(getApplicationContext(), result.getProperty(0).toString(), Toast.LENGTH_LONG).show();

                }

                else
                {
                   Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
                }

          } catch (Exception e) {

                e.printStackTrace();
       }
      }
     });

  buttonBrowse = (Button) findViewById(R.id.buttonBrowse);
  buttonBrowse.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(position!= null){
                showPopup(MainActivity.this, position);
            }           
        }

        @SuppressWarnings("deprecation")
        private void showPopup(final Activity activity, Point position) {

            int popupWidth = 120;
            int popupHeight = 130;

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.popup);

            LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);

             popupView = layoutInflater.inflate(R.layout.activity_popup, linearLayout);

             popupWindow = new PopupWindow(activity);
             popupWindow.setContentView(popupView);
             popupWindow.setWidth(popupWidth);
             popupWindow.setHeight(popupHeight);

             int offset_X = 85;
             int offset_Y = 5;

             popupWindow.setBackgroundDrawable(new BitmapDrawable());
             popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY, position.x + offset_X, position.y + offset_Y);

        }
    });     
}

@Override
public void onWindowFocusChanged(boolean hasFocus){

    int[] location = new int[2];
    buttonBrowse.getLocationOnScreen(location);
    position = new Point();
    position.x = location[0];
    position.y = location[1];

}

public void buttonGallery_Click(View v){

    Intent intent = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQ_CODE_PICK_IMAGE);
}

public void buttonTakePhoto_Click(View v){
        TakePhoto();
}

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
           Intent intent){

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  

         selectedImageUri = intent.getData();
         selectedImageRealPath = getRealPathFromURI(selectedImageUri);
         String path = selectedImageRealPath;
         imageName = path.substring(path.lastIndexOf("/")+1, path.length()); 
         imageSelected = BitmapFactory.decodeFile(selectedImageRealPath);
         final ImageView imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
         imageViewPhoto.setImageBitmap(imageSelected);
         compressedImageString = imageCompression(selectedImageRealPath); 

        }
        break;

    case CAMERA_REQUEST:
    try
    {
        if(resultCode == RESULT_OK){
            selectedImageRealPath = getRealPathFromURI(selectedImageUri);
            imageSelected = BitmapFactory.decodeFile(selectedImageRealPath);
            final ImageView imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
            imageViewPhoto.setImageBitmap(imageSelected);
            compressedImageString = imageCompression(selectedImageRealPath);

        }
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
        break;
    }   

    popupWindow.dismiss();  
}

private void TakePhoto() {

    ContentValues values = new ContentValues();
    imageName = String.valueOf(System.currentTimeMillis());
    values.put(MediaStore.Images.Media.TITLE, imageName);
    selectedImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            values);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
    startActivityForResult(intent, CAMERA_REQUEST);

}

private String imageCompression(String filePath) {
    File imageFile = new File(filePath);
    FileInputStream fis = null;

    try
    {
      fis = new FileInputStream(imageFile); 
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }

    Bitmap bitmap = BitmapFactory.decodeStream(fis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 600, 300, false);
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos );
    byte[] b = baos.toByteArray();
    String imageString = Base64.encodeToString(b, Base64.DEFAULT);
    //byte[] imageByte = imageString.getBytes();

   return imageString;

}

public String getRealPathFromURI(Uri contentUri)
{
    try
    {
        String[] proj = {MediaStore.Images.Media.DATA};
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    catch (Exception e)
    {
        return contentUri.getPath();
    }
}

}

我想从c#调用buttonBrowse上的方法

最佳答案

您不能直接从 C# 代码调用 java 代码。

您可以将 java 类包装为 COM

请参阅下面的链接

http://www.rgagnon.com/javadetails/java-0045.html

关于c# - 如何从c#代码中调用java中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14809360/

相关文章:

c# - 有什么方法可以调用这个 getter 并始终如一地获得预期结果吗?

c# - 防止触发 CheckBox Checked 事件

c# - c#中使用single方法会出现Stackoverflow异常吗?

android - 如何使 TextView 在 api 11 下可选择

android - 获取没有标题/通知栏的窗口大小

c# - 在开源应用程序中存储已保存的密码

java - 数据库分布

java - 我正在尝试使用 MouseMotionListener 将数据从 JTable 打印到命令行

java - kubernetes 上的 Spark 作业失败,没有特定错误

Android: "last level"的作弊预防