Get file:
public void Init ()
{
Noscrollgridview = (GridView) Findviewbyid (R.id.noscrollgridview);
Noscrollgridview.setselector (New colordrawable (color.transparent));
adapter = new Gridadapter (this);
Adapter.update1 ();
Noscrollgridview.setadapter (adapter);
Noscrollgridview.setonitemclicklistener (New Onitemclicklistener ()
{
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
Long Arg3)
{
Inputmethodmanager IMM = (Inputmethodmanager) arg1.getcontext (). Getsystemservice (Context.input_method_service);
Imm.hidesoftinputfromwindow (Arg1.getapplicationwindowtoken (), 0);
if (arg2 = = Bimp.bmp.size ())
{
New Popupwindows (Publishedactivity.this, Noscrollgridview);
}
Else
{
Intent Intent = new Intent (Publishedactivity.this,
Photoactivity.class);
Intent.putextra ("ID", arg2);
StartActivity (Intent);
}
}
});
Activity_selectimg_send = (Button) Findviewbyid (r.id.activity_selectimg_send);
Activity_selectimg_send.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
ShowDialog ("ddd");
Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()
. Permitnetwork (). build ());
list<string> list = new arraylist<string> ();
for (int i = 0; i < Bimp.drr.size (); i++)
{
String Str = Bimp.drr.get (i). SUBSTRING (
Bimp.drr.get (i). LastIndexOf ("/") + 1,
Bimp.drr.get (i). LastIndexOf ("."));
List.add (Fileutils.sdpath + Str + "). JPEG ");
String Path = Bimp.drr.get (i);
ShowDialog (path);
ShowDialog (Getdatavalue ("_member_id"));
ShowDialog ((TextView) Findviewbyid (R.id.activity_selectxxx_send)). GetText (). toString ());
String Filekey = "Imgfile";
Uploadutil uploadutil = Uploadutil.getinstance ();
Uploadutil.setonuploadprocesslistener (Publishedactivity.this); Set Listener listening upload status
map<string, string> params = new hashmap<string, string> ();
Params.put ("userid", Getdatavalue ("_member_id"));
Params.put ("Content", ((TextView) Findviewbyid (R.id.activity_selectxxx_send)). GetText (). toString ());
Uploadutil.uploadfile (Path,filekey, "http://shop.tminji.com/sns/SendAMessage", params);
String x = ((TextView) V.findviewbyid (R.id.activity_selectxxx_send)). GetText (). toString ();
UploadFile (path);
}
HD compressed images are all in the list path.
High-definition compressed BMP objects are inside the bimp.bmp.
After completing the upload server ...
ShowDialog ("published successfully ^_^" ");
Fileutils.deletedir ();
Intent Intent = new Intent (publishedactivity.this, Mainactivity.class);
Bundle bundle = new bundle ();
/* characters, strings, Booleans, byte arrays, floating-point numbers, and so on, can be passed */
Bundle.putstring ("Name", "feng88724");
/* Assign the Bundle object to intent*/
//
Intent.setclass (Publishedactivity.this, Mainactivity.class);
Intent.putextras (bundle);
StartActivity (Intent);
Intent intentx = new Intent (publishedactivity.this, Mainactivity.class);
/* Stores the data that needs to be passed through the bundle object */
Bundle bundle = new bundle ();
/* characters, strings, Booleans, byte arrays, floating-point numbers, and so on, can be passed */
Bundle.putstring ("Name", "feng88724");
/* Assign the Bundle object to intent*/
Intentx.putextras (bundle);
StartActivity (INTENTX);
}});
}
Uploading files
private void Touploadfile (file file, string Filekey, String Requesturl,
map<string, string> param) {
String result = null;
Requesttime= 0;
Long requesttime = System.currenttimemillis ();
Long responsetime = 0;
try {
URL url = new URL (requesturl);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setreadtimeout (readtimeout);
Conn.setconnecttimeout (ConnectTimeout);
Conn.setdoinput (TRUE); Allow input stream
Conn.setdooutput (TRUE); Allow output stream
Conn.setusecaches (FALSE); Caching is not allowed
Conn.setrequestmethod ("POST"); Request method
Conn.setrequestproperty ("Charset", Charset); Set encoding
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setrequestproperty ("Content-type", Content_Type + "; boundary=" + boundary);
Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
/**
* When the file is not empty, the file is packaged and uploaded
*/
DataOutputStream dos = new DataOutputStream (Conn.getoutputstream ());
StringBuffer SB = null;
String params = "";
/***
* The following is for uploading parameters
*/
if (param! = null && param.size () > 0) {
Iterator<string> it = Param.keyset (). Iterator ();
while (It.hasnext ()) {
SB = null;
SB = new StringBuffer ();
String key = It.next ();
String value = Param.get (key);
Sb.append (PREFIX). Append (Boundary). Append (Line_end);
Sb.append ("Content-disposition:form-data; Name=\ ""). Append (Key). Append ("\" "). Append (Line_end). Append (Line_end);
Sb.append (value). Append (Line_end);
params = sb.tostring ();
LOG.I (TAG, key+ "=" +params+ "# #");
Dos.write (Params.getbytes ());
Dos.flush ();
}
}
SB = null;
params = null;
SB = new StringBuffer ();
/**
* Here the Key NOTE: The value inside the name is the server side need key only this key can get the corresponding file
* filename is the name of the file, including the suffix name, for example: Abc.png
*/
Sb.append (PREFIX). Append (Boundary). Append (Line_end);
Sb.append ("Content-disposition:form-data; Name=\ "" + Filekey
+ "\"; Filename=\ "" + file.getname () + "\" "+ line_end);
Sb.append ("Content-type:image/pjpeg" + line_end); The Content-type configured here is important for server-side identification of the type of file
Sb.append (Line_end);
params = sb.tostring ();
SB = null;
LOG.I (TAG, File.getname () + "=" + params+ "# #");
Dos.write (Params.getbytes ());
/** Uploading Files */
InputStream is = new FileInputStream (file);
Onuploadprocesslistener.initupload ((int) file.length ());
byte[] bytes = new byte[1024];
int len = 0;
int curlen = 0;
while (len = is.read (bytes))! =-1) {
Curlen + = Len;
Dos.write (bytes, 0, Len);
Onuploadprocesslistener.onuploadprocess (Curlen);
}
Is.close ();
Dos.write (Line_end.getbytes ());
Byte[] End_data = (PREFIX + boundary + PREFIX + line_end). GetBytes ();
Dos.write (End_data);
Dos.flush ();
Dos.write (Tempoutputstream.tobytearray ());
/**
* Get response Code 200 = Success When response is successful, get response stream
*/
int res = Conn.getresponsecode ();
ResponseTime = System.currenttimemillis ();
This.requesttime = (int) ((responsetime-requesttime)/1000);
LOG.E (TAG, "Response code:" + res);
if (res = = 200) {
LOG.E (TAG, "request Success");
InputStream input = Conn.getinputstream ();
StringBuffer sb1 = new StringBuffer ();
int SS;
while (ss = Input.read ())! =-1) {
Sb1.append ((char) SS);
}
result = Sb1.tostring ();
LOG.E (TAG, "Result:" + result);
SendMessage (Upload_success_code, "Upload results:"
+ result);
Return
} else {
LOG.E (TAG, "request Error");
SendMessage (Upload_server_error_code, "Upload failed: code=" + res);
Return
}
} catch (Malformedurlexception e) {
SendMessage (Upload_server_error_code, "Upload failed: error=" + e.getmessage ());
E.printstacktrace ();
Return
} catch (IOException e) {
SendMessage (Upload_server_error_code, "Upload failed: error=" + e.getmessage ());
E.printstacktrace ();
Return
}
}
Android upload file, HTTP submission Form