Sunday, January 26, 2014

Android calls. Net webservice picture upload


            
webservice code
public string FileUploadImage (string bytestr)
{
string name = "";
string mess = "";
try
{
/ / Random random = new Random ();
/ / String i = random.Next (0, 10000000) ToString ();.
name = DateTime.Now.Year.ToString () + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
bool flag = StringToFile (bytestr, Server.MapPath ("image \ \") + "" + name + "jpg".);
string filePath = "/ image /" + name + "jpg.";
}
catch (Exception ex)
{
mess = ex.Message;
}
if (mess! = "")
{
return mess;
}
else
{
return "File uploaded successfully";
}
}
protected System.Drawing.Image Base64StringToImage (string strbase64)
{
try
{
byte [] arr = Convert.FromBase64String (strbase64);
MemoryStream ms = new MemoryStream (arr);
/ / Bitmap bmp = new Bitmap (ms);

ms.Write (arr, 0, arr.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream (ms);
ms.Close ();
return image;
/ / Return bmp;
}
catch (Exception ex)
{
throw ex;
}
}
/ / /
/ / / Save after base64 encoded string to file
/ / /

/ / / base64 coded by the string after
/ / / save the file path and file name
/ / / Save the file if successful
public static bool StringToFile (string base64String, string fileName)
{
/ / String path = Path.GetDirectoryName (.. Assembly.GetExecutingAssembly () GetName () CodeBase) + @ "/ beapp /" + fileName;

System.IO.FileStream fs = new System.IO.FileStream (fileName, System.IO.FileMode.Create);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter (fs);
if (! string.IsNullOrEmpty (base64String) && File.Exists (fileName))
{
bw.Write (Convert.FromBase64String (base64String));
}
bw.Close ();
fs.Close ();
return true;
}
java code
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
t1 = (TextView) findViewById (R.id.t1);
image = (ImageView) this.findViewById (R.id.image);
button = (Button) findViewById (R.id.button);
button.setOnClickListener (new WebOnclick ());
button1 = (Button) this.findViewById (R.id.button1);
button1.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
Intent intent = new Intent (Intent.ACTION_PICK, null);
intent.setDataAndType (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult (intent, PHOTOZOOM);
}
});
}
class WebOnclick implements OnClickListener {

@ Override
public void onClick (View v) {
/ / ResponseVote ();
/ / T1.setText (METHOD_NAME);

if (testUpload ())
t1.setText ("Image uploaded successfully!");
else
t1.setText ("Image upload failed!");
}
}
@ SuppressLint ("SdCardPath")
public boolean testUpload () {
try {
String srcUrl = "/ sdcard /"; / / Path
String fileName = "19.jpg"; / / filename
FileInputStream fis = new FileInputStream (srcUrl + fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
byte [] buffer = new byte [8192];
int count = 0;
while ((count = fis.read (buffer))> = 0) {
baos.write (buffer, 0, count);
}
String uploadBuffer = new String (Base64.encode (baos.toByteArray ())); / / perform Base64 encoding
/ / String methodName = "FileUploadImage";
connectWebService (uploadBuffer); / / call the webservice
Log.i ("connectWebService", "start");
fis.close ();
return true;
} Catch (Exception e) {
e.printStackTrace ();
}
return false;
}
private boolean connectWebService (String imageBuffer) {
String namespace = "http://tempuri.org/";
/ / Namespace that the server was interfaces Note: suffix did not add wsdl,
.String url = "http://192.168.2.100:55236/Service1.asmx";
String methodName = "FileUploadImage";
/ / Corresponding url
/ / The following is the calling process, and do not understand, then please see the relevant documents
webserviceSoapObject soapObject = new SoapObject (namespace, methodName);
/ / SoapObject.addProperty ("filename", fileName); / / Parameter 1 picture name
soapObject.addProperty ("image", imageBuffer); / / Parameter 2 picture string
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.setOutputSoapObject (soapObject);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.ENC;
@ SuppressWarnings ("deprecation")
AndroidHttpTransport httpTranstation = new AndroidHttpTransport (url);
try {
httpTranstation.call (namespace, envelope);
Object result = envelope.getResponse ();
Log.i ("connectWebService", result.toString ());
if (result.toString () == "File uploaded successfully")
return true;
else
return false;
} Catch (Exception e) {
e.printStackTrace ();
}
return false;
}
After the test run without error and prompts pictures uploaded successfully, but in the image catalog and no pictures, please look at the problem lie

Reply:
/ Sdcard / should be / mnt / sdcard / bar.
Reply:
cited a floor reply:
/ sdcard / should be / mnt / sdcard / bar.

Tried or not ah

Reply:
Will lz resolved yet? Can not communicate, I QQ 414215875
Reply:
If more than one picture, then how do ah
Reply:
The next question you want to solve it? I have encountered this problem

Reply:
reference to the second floor huiyan1023 reply:
[Quote = reference to a floor reply:]
/ Sdcard / should be / mnt / sdcard / bar.

Tried or not ah

Reply:
Landlord resolved, but not kind ah

No comments:

Post a Comment