如何使用web api将图像和文本从android应用程序发送到sql server

如何使用web api将图像和文本从android应用程序发送到sql server,android,asp.net-web-api,Android,Asp.net Web Api,我可以单独发送图像和文本,但我想同时发送图像和文本 请帮助任何人使用multipart。使用multipart,您可以使用文本数据发送图像、视频和其他文件。各种库可用于多部分发送数据。Ion就是其中之一一种方法是您可以在web api中将图像转换为Base64字符串 public static String imageToString(Bitmap BitmapData) { ByteArrayOutputStream bos = new ByteArrayOutputStre

我可以单独发送图像和文本,但我想同时发送图像和文本
请帮助任何人使用multipart。使用multipart,您可以使用文本数据发送图像、视频和其他文件。各种库可用于多部分发送数据。Ion就是其中之一

一种方法是您可以在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中传递字符串参数,然后在服务器端对这些字符串进行解码

特别感谢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;
}
private void saveText(){
字符串图像=getStringImage(旋转BMP);
ImageCapture=新的ImageCapture();
imageCapture.Name=prescriptionName.getText().toString();
imageCapture.Comments=Comments.getText().toString();
imageCapture.ImageURL=mCurrentPhotoPath;
imageCapture.PhotoName=PhotoName;
imageCapture.Image=Image;
imageCapture.Id=\u ImageId\u Id;
if(_ImageId_Id==0){
restService.getService().InsertPrescription(imageCapture,新回调函数(){
@凌驾
public void成功(图像捕获、响应){
Toast.makeText(Prescription.this,“插入新记录”,Toast.LENGTH_LONG.show();
Intent Intent=newintent(getApplicationContext(),Home.class);
星触觉(意向);
}
@凌驾
公共无效失败(错误){
Toast.makeText(Prescription.this,error.getMessage().toString(),Toast.LENGTH_LONG.show();
}
});
}
}
私人照片{
DispatchTakePictureContent();
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
Log.i(标记为“onActivityResult:+this”);
if(requestCode==请求\拍照&&resultCode==活动。结果\确定){
setPic();
}
}
串电流光路;
字符串名称;
静态最终int请求\u拍摄\u照片=1;
文件photoFile=null;
私有无效DispatchTakePictureContent(){
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
//确保有摄像头活动来处理意图
if(takePictureContent.resolveActivity(getPackageManager())!=null){
//创建照片应该放在哪里的文件
文件photoFile=null;
试一试{
photoFile=createImageFile();
}捕获(IOEX异常){
//创建文件时出错
}
//仅当成功创建文件时才继续
if(photoFile!=null){
takePictureContent.putExtra(MediaStore.EXTRA_输出,
fromFile(photoFile));
startActivityForResult(拍摄内容、请求拍照);
}
}
}
公共字符串getStringImage(位图bmp){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[]imageBytes=bas.toByteArray();
字符串encodedImage=Base64.encodeToString(imageBytes,Base64.DEFAULT);
返回图像;
}
有关详细信息[如何使用web api将图像和文本从android应用程序发送到sql server][1]


请在此处发布问题之前进行搜索