1

I'm sending image from Android to C# webservice. I always get a black image.Can anyone help?

Android side:

 public void save(View v) {           
        mBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);           
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG,40, outputStream);
        byte[] imgByte = outputStream.toByteArray();
        String base64Str = Base64.encodeToString(imgByte, Base64.DEFAULT);
       // Send base64Str to server
}

C# side:

[WebMethod]
public void GetImage(string base64ImageStr)
{           
      byte[] imageBytes = Convert.FromBase64String(base64ImageStr);
      //Save imageBytes to DB                
 }

I'm calling it from DB:

 public ActionResult Image(){
        var bytes=GetBytesArrayFromDB(id);
        return File(bytes, "image/jpeg");
    }

in View:

<img src='@Url.Action("Image")' alt="" />
2
  • Hi @zaza. I also be interested in this problem. But, I'm having trouble with sending the base64 string to webservice. Because, base64 encoded string length about ~103.000 chars. So, HTTP standarts aren't allow this length (max length of URL is 2000). Can you share parts of code like: // Send base64Str to server and //Save imageBytes to DB . Thanks. Commented Jul 3, 2015 at 20:07
  • Hi @MirjalalTalishinski. I'm using soap web services Commented Jul 10, 2015 at 21:31

1 Answer 1

1

I figure it out I didn't Canvas it before sending it to server. use this too

Canvas canvas = new Canvas(mBitmap); v.draw(canvas);

 public void save(View v) {           
            mBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);           
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            mBitmap.compress(Bitmap.CompressFormat.JPEG,40, outputStream);
            byte[] imgByte = outputStream.toByteArray();
            String base64Str = Base64.encodeToString(imgByte, Base64.DEFAULT);

            Canvas canvas = new Canvas(mBitmap);
            v.draw(canvas);

           // Send base64Str to server
    }

//send base64Str to server

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap
{
    public final String SOAP_ACTION = //url;
    public  final String OPERATION_NAME = "name of action";
    public  final String WSDL_TARGET_NAMESPACE = "asmx url";
    public  final String SOAP_ADDRESS = "soap address";
public CallSoap()
{
}
public String Call(String base64Str)
{
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

    PropertyInfo pi=new PropertyInfo();
    pi.setName("base64Str");
    pi.setValue(base64Str);
    pi.setType(String.class);
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response=null;
    try
    {
        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
    }
    catch (Exception exception)
    {
        response=exception.toString();
    }
    return response.toString();
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.