Hello I am new in the Android development. I want to know how to upload an image in android I did not find any useful tutorial for this. Can you give me some instructions, pls, help me
I have created these Lill methods for you:
Personal Boolean handle diagram (string filp, string mimetype) {HttpURL connection connection = zero; DataOutputStream Outstream = Faucet; DataInputStream Instream = Faucet; String lineend = "\ r \ n"; String doublefinance = "-"; String border = "*****"; Int bytes read, bytes available, buffer size; Byte [] buffer; Int Max buffer size = 1 * 1024 * 1024; String urlString = "http://www.yourwebserver.com/youruploadscript.php"; {Try FileInputStream file InputStream = null; {FileInputStream = Try New FileInputStream (New File (FilePath)); } Hold (FileNotFoundException e) {} URL url = new URL (url string); Connection = (HttpURL connection) url.openConnection (); Connection.setDoInput (true); Connection.setDoOutput (true); Connection.setUseCaches (wrong); Connection.setRequestMethod ("post"); Connection.setRequestProperty ("Connection", "Keep-Elive"); Connection.setRequestProperty ("content-type", "multipart / form-data; limit =" + limit); Outstream = New DataOutputStream (connection.getOutputStream ()); Outstream.writebytes (AddParam ("Somaparam", "Some Ultimate Content", Two Hippens, Borders, Lines)); Outstream.writebytes (two hippens + border + lined); Outstream.litabytes ("Content-Disposal: Form-Data; Name = \" Uploaded file \ "; filename = \" + + "+" + "\" + "+ +" Content Type: "+ MimeType + Lineand + "Content-transfer encoding: binary" + lineup "); outStream.writeBytes (lineEnd); bytes available = fileInputStream.available (); buffer size = math (bytes available, maxbuffer size); buffer = new byte [buffer size] ; BytesRed = FileInputStreamRed (buffer, 0, buffer size); while (bytes) D & gt; 0) {outstream.write (buffer, 0, buffer size); bytes available = fileInputStream.available (); buffer size = math. (Bytes available, maxbuffer size); bytesread = fileinputstream.read (buffer, 0 , Buffer size);} Outstream.writebytes (lineand); Outstream.writebytes (two hpens + range + twofifens + lineand); fileInputStream.close (); outStream.flush (); outStream.close ();} hold (malformedlexception E) {Log.e ("DEBUG", "[MalformedULException during sending a picture"]); hold (IOException e) {Log.e ("DEBUG", "[IOException while sending a picture"]); } {InStream = Try New DataInputStream (connection.getInputStream ()); Letter string; While ((str = inStream.readLine ()) = = null {if (str == "1") {back true;} other {return false};} InStream.close ();} hold (IOException e) {log .e ("DEBUG", "[IOException while sending a picture and receiving response]");} The description is false;} Private string addParam (string key, string value, string doublehinfence, string border, string lines) {return Two Hippens + Boundary + Line End + "Content-Presentation: Form-Data; Name = \" + + "+" + "\" + LINE + LINE + MONEY + LINEAND;}
So far, you need some PHP scripts on your webserver which gives "1" for a successful upload and something else for an error. I also block the user during upload To prevent this, I suggest doing this in an ASINCTs. In the webserver, you have got a file named "uploaded file", hope that helps!
Comments
Post a Comment