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

时间:2013-02-11 09:36:57

标签: 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上的方法

1 个答案:

答案 0 :(得分:1)

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

您可以将java类包装为COM

请参阅以下链接

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