Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code I get bytes null where i used this to encode the string of image from android and i am decoding from here , i want to upload image in drive.



C#
[WebMethod]
   public string ImageConvert(string _FromString)
   {
       string MailFile = "";
       try
       {

           string s_bitImage = _FromString;
           <big></big>byte[] bytes = Convert.FromBase64String(s_bitImage);
           System.Drawing.Bitmap image;

           string fileName = "";
           string FolderLocation = System.Web.Configuration.WebConfigurationManager.AppSettings["G:/serverdata/"].ToString();


           using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
           {
               fileName = Guid.NewGuid().ToString();
               image = (Bitmap)System.Drawing.Bitmap.FromStream(ms);

               MailFile = FolderLocation + fileName + ".png";
               image.Save(Server.MapPath(MailFile));
           }
       }
       catch (Exception ex)
       {
           MailFile = ex.Message;
       }


       return MailFile;

   }
Posted
Comments
[no name] 14-Aug-12 9:08am    
Why are you not checking to see if _FromString is a valid string?
Sergey Alexandrovich Kryukov 14-Aug-12 12:08pm    
It would throw an exception, that's why. Therefore, I'm not sure bytes is null; and I'm unsure where is the exception. And there is no a way to debug it without a sample of "_FromString".
--SA
AndroidVivek 16-Aug-12 4:38am    
Input stream is not coming null i checked it out and you can also check from below code.
Sergey Alexandrovich Kryukov 16-Aug-12 12:52pm    
Then I don't quite understand what's going on, probably I missed something...
--SA

Instead of making a memory stream you can write the image as:

C#
using (FileStream fsNew = new FileStream(Server.MapPath(MailFile), FileMode.Create, FileAccess.Write))
            {
                try
                {
                    fsNew.Write(bytes, 0, bytes.Length);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }



Also if you are specific to MemoryStream, flush it before using it.
C#
using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length))
           {
               fileName = Guid.NewGuid().ToString();

ms.Flush();

C#
    image = (Bitmap)System.Drawing.Bitmap.FromStream(ms);

    MailFile = FolderLocation + fileName + ".png";
    image.Save(Server.MapPath(MailFile));
}
 
Share this answer
 
v3
hey still get error , would you can help any more..i need to upload photo in sever via android phone...

this is my android side code which is working encoding....


bm = BitmapFactory.decodeResource( getResources(),R.drawable.images);
bmImage.setImageBitmap(bm);

outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();


// Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b,Base64.DEFAULT);

// Toast.makeText(getApplicationContext(),"ENCODED" + encodedImage, 1).show();

SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

//Toast.makeText(getApplicationContext(),"request " + request, 1).show();

request.addProperty("_FromString",encodedImage);

Toast.makeText(getApplicationContext(),"request2 " + request.addProperty("_FromString",encodedImage), 1).show();

SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);

//Toast.makeText(getApplicationContext(),"encode " + encodedImage, 1).show();

//Toast.makeText(getApplicationContext(),"Envelop " + envelop, 1).show();

envelop.dotNet=true;
envelop.setOutputSoapObject(request);

HttpTransportSE aht = new HttpTransportSE(URL);

//Toast.makeText(getApplicationContext(),"aht " + aht, 1).show();

aht.call(SOAP_ACTION, envelop);

SoapPrimitive result = (SoapPrimitive)envelop.getResponse();

String s = result.toString();

Toast.makeText(getApplicationContext(),"Response " +s, 1).show();
 
Share this answer
 
Are you sure bytes is null? AFAIK, it is initialized but not null only in the cases where input string is an empty string. In cases where input parameter is null or if it is not a valid base64 string, it will throw exception.

I would suggest checking the value you are providing to the method. I suspect it is empty string.

Also, please do not post replies to your own question. Update your question with additional information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900