2017-04-27 50 views

回答

0

使用多發送圖片和文字從Android應用程序到SQL Server。使用多部分,您可以將圖像視頻和其他文件與您的文本數據一起發送。各種庫可用於以多部分發送數據。離子就是其中之一

0

一種方法是你可以在圖像轉換爲Base64編碼字符串中的Web API

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和解碼的字符串在服務器端

+0

由於Bhupat Bheda我的項目之前,請搜索徹底完成 –

0

感謝你們所有人特別感謝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