简体   繁体   中英

How to call method in java from c# code?

I have written a method in android(java) ie is used to upload image to the server. I want this method to be called from c# code. How to go about i have no idea, the java code follows below?

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();
    }
}

}

I want to call the method on buttonBrowse from c#

You cannot call java code directly from C# code.

You can wrap java classes as COM

See the link below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM