【问题标题】:How to send Image and text from android application to sql server using web api如何使用 web api 将图像和文本从 android 应用程序发送到 sql server
【发布时间】:2017-09-24 18:34:11
【问题描述】:

我可以单独发送图片和文字,但我想同时发送图片和文字 请任何人帮助我

【问题讨论】:

  • 在此处发布问题前请先搜索

标签: android asp.net-web-api


【解决方案1】:

使用多部分。使用多部分,您可以使用文本数据发送图像视频和其他文件。各种库可用于多部分发送数据。离子就是其中之一

【讨论】:

    【解决方案2】:

    一种方法是您可以在 web api 中将图像转换为 Base64 字符串

    public static String imageToString(Bitmap BitmapData) {
    
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            BitmapData.compress(Bitmap.CompressFormat.PNG, 100, bos);
            byte[] byte_arr = bos.toByteArray();
    
            String file = Base64.encodeToString(byte_arr, Base64.DEFAULT);
            //appendLog(file);
            return file;
    }
    

    您可以在上述函数中将位图图像转换为 Base64 字符串,并在您的 api 中传递字符串参数并在服务器端解码这些字符串

    【讨论】:

      【解决方案3】:

      感谢大家,特别感谢 Bhupat Bheda。我完成了我的全部项目。现在我想分享我的研究。

      private void saveText() {
      
          String image= getStringImage(rotatedBMP);
          ImageCapture imageCapture = new ImageCapture();
      
          imageCapture.Name = prescriptionName.getText().toString();
          imageCapture.Remarks = remarks.getText().toString();
          imageCapture.ImageURL=mCurrentPhotoPath;
          imageCapture.PhotoName=photoName;
          imageCapture.Image=image;
      
          imageCapture.Id = _ImageId_Id;
          if (_ImageId_Id == 0) {
              restService.getService().InsertPrescription(imageCapture, new Callback<ImageCapture>() {
                  @Override
                  public void success(ImageCapture imageCapture, Response response) {
                      Toast.makeText(Prescription.this, "New Record Inserted.", Toast.LENGTH_LONG).show();
      
                      Intent intent=new Intent(getApplicationContext(),Home.class);
                      startActivity(intent);
                  }
      
                  @Override
                  public void failure(RetrofitError error) {
                      Toast.makeText(Prescription.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
      
                  }
              });
          }
      
      }
      
      private void takePhoto() {
          dispatchTakePictureIntent();
      }
      
      
      
      
      
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // TODO Auto-generated method stub
          Log.i(TAG, "onActivityResult: " + this);
          if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
              setPic();
          }
      }
      
      String mCurrentPhotoPath;
      String photoName;
      
      static final int REQUEST_TAKE_PHOTO = 1;
      File photoFile = null;
      
      private void dispatchTakePictureIntent() {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          // Ensure that there's a camera activity to handle the intent
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
              // Create the File where the photo should go
              File photoFile = null;
              try {
                  photoFile = createImageFile();
              } catch (IOException ex) {
                  // Error occurred while creating the File
      
              }
              // Continue only if the File was successfully created
              if (photoFile != null) {
                  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                          Uri.fromFile(photoFile));
                  startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
              }
          }
      }
      
      
      
      public String getStringImage(Bitmap bmp) {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
          byte[] imageBytes = baos.toByteArray();
          String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
          return encodedImage;
      }
      

      了解详情[如何使用 web api 将图像和文本从 android 应用程序发送到 sql server][1]

      https://esoftpanel.blogspot.com/2017/04/how-to-send-image-and-text-from-android.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多