我需要在android中上传图片

编程入门 行业动态 更新时间:2024-10-27 02:19:43
本文介绍了我需要在android中上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

HI 谁能告诉我如何在android ....中将图片从USB上传到数据库,并使用按钮上传来获取该图像.

HI can anyone tell me how to upload a pic in android....from a USB to a Database and using a button upload to get that image

推荐答案

您可以尝试以下代码: You can try the below code: private boolean handlePicture(String filePath, String mimeType) { HttpURLConnection connection = null; DataOutputStream outStream = null; DataInputStream inStream = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; String urlString = "www.yourwebserver/youruploadscript.php"; try { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(new File(filePath)); } catch(FileNotFoundException e) { } URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); outStream = new DataOutputStream(connection.getOutputStream()); outStream.writeBytes(addParam("someparam", "content of some param", twoHyphens, boundary, lineEnd)); outStream.writeBytes(twoHyphens + boundary + lineEnd); outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd); outStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outStream.writeBytes(lineEnd); outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); fileInputStream.close(); outStream.flush(); outStream.close(); } catch (MalformedURLException e) { Log.e("DEBUG", "[MalformedURLException while sending a picture]"); } catch (IOException e) { Log.e("DEBUG", "[IOException while sending a picture]"); } try { inStream = new DataInputStream( connection.getInputStream() ); String str; while (( str = inStream.readLine()) != null) { if(str=="1") { return true; } else { return false; } } inStream.close(); } catch (IOException e){ Log.e("DEBUG", "[IOException while sending a picture and receiving the response]"); } return false; } private String addParam(String key, String value, String twoHyphens, String boundary, String lineEnd) { return twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd + value + lineEnd; }

更多推荐

我需要在android中上传图片

本文发布于:2023-11-27 08:46:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!