上传多张图片到服务器

编程入门 行业动态 更新时间:2024-10-20 18:55:04
本文介绍了上传多张图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

用户可以在Android的侧面照片5-6通过摄像头。所以,我用ACTION_IM​​AGE_CAPTURE。在的onActivityResult我这样做是为了收集由照相机拍摄的图像的位图。假设为第一个采取事先知情同意和第二PIC采取如下。

User can take 5-6 pictures in android side through camera. So, I used ACTION_IMAGE_CAPTURE. In the onActivityResult I do this to collect the bitmap of the image taken by the camera. Suppose for first taken pic and second taken pic as below.

if(requestCode == 1) { bitMap1 = (Bitmap)extras.get("data"); imageView1.setImageBitmap(bitMap1); globalvar = 2; } if(requestCode == 2) { bitMap1 = (Bitmap)extras.get("data"); imageView2.setImageBitmap(bitMap2); globalvar = 2; }

为了这些图像发送到PHP服务器,我做以下..

protected String doInBackground(Integer... args) { // Building Parameters ByteArrayOutputStream bao1 = new ByteArrayOutputStream(); bitMap1press(Bitmap.CompressFormat.JPEG, 90, bao1); byte [] bytearray1 = bao1.toByteArray(); String stringba1 = Base64.encode(bytearray1); ByteArrayOutputStream bao2 = new ByteArrayOutputStream(); bitMap2press(Bitmap.CompressFormat.JPEG, 90, bao2); byte [] bytearray2 = bao2.toByteArray(); String stringba2 = Base64.encode(bytearray2); String parameter1 = "tenant"; String parameter2 = "price"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("person",parameter1)); params.add(new BasicNameValuePair("price",parameter2)); params.add(new BasicNameValuePair("image1",stringba1)); params.add(new BasicNameValuePair("image2",stringba2)); JSONObject json = jParser.makeHttpRequest(requiredurl, "POST", params); Log.d("Details", json.toString()); int success = json.getInt("connected"); if (success == 1) { //blah blah } }

下面是在 makeHtt prequest()方法

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { // Making HTTP request try { // check for request method if(method == "POST"){ // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } ................ ....................... // Here the result is extracted and made it to json object ............................. // return JSON return jObj; // returning the json object to the method that calls. }

下面是PHP code片断:

$name = $_POST[person]; $price = $_POST[price]; $binary1 = base64_decode($image2); $binary2 = base64_decode($image2); $file1 = tempnam($uploadPath, 'image2'); $fp1 = fopen($file1, 'wb'); fwrite($fp1, $binary1); fclose($fp1); ................. ............................

但我不能存储在SEVER端文件夹的图像。即使我已经看到了一些链接说Base64是不是preferable方式上载多张图像。是否有人可以建议我如何进行?见过this等多个环节,但不能得到如何在我的要求出发,因为我有,甚至发送一些数据(如人名,价格)与图像一起。任何帮助是极大的AP preciated。

However I am unable to store images in sever side folder. Even I have seen in some links saying that Base64 is not preferable way while uploading multiple images. Can someone please suggest me how to proceed? Have seen this and many other links, but can't get how to proceed in my requirement as I have to even send some data(like person name, price) along with that images. Any help on this is greatly appreciated.

注意:即使有人可以建议我如何保存在上面的服务器文件夹临时文件($文件1),我会非常感激。

NOTE: Even if someone can suggest me how to save that above temp file($file1) in the server folder, I would be very thankful.

推荐答案

下面是我的code片段,希望这有助于:

Here is my code snippet, Hope this helps :

private class AsyncTask1 extends AsyncTask<Void, Void, String>{ @Override protected String doInBackground(Void... params) { boolean response = false; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileBody bin = new FileBody(new File("temp")); File tempImg = new File("sdcard/signature.jpg"); if(tempImg.exists()) { checkimgfile=checkimgfile+"LPA"+tempImg; bin = new FileBody(tempImg, "image/jpg"); reqEntity.addPart("txt_sign_lpa", bin); reqEntity.addPart("count_lpa",new StringBody("1")); } else { reqEntity.addPart("count_lpa",new StringBody("0")); } FileBody bin1 = new FileBody(new File("temp")); File tempImg1 = new File("sdcard/signature2.jpg"); if(tempImg1.exists()) { checkimgfile=checkimgfile+"subject"+tempImg1; bin1 = new FileBody(tempImg1, "image/jpg"); reqEntity.addPart("txt_sign", bin1); reqEntity.addPart("count_subject",new StringBody("1")); } reqEntity.addPart("count",new StringBody("0")); reqEntity.addPart("name",new StringBody("Shaili")); reqEntity.addPart("age",new StringBody("47")); try { ConnectivityManager cm = (ConnectivityManager)getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if(activeNetwork!=null && activeNetwork.isAvailable() && activeNetwork.isConnected()) { String xml = ""; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 100000); HttpConnectionParams.setSoTimeout(httpParameters, 100000); final HttpClient httpclient = new DefaultHttpClient(httpParameters); final HttpPost httppost = new HttpPost("www.xyz/abc.php");//url where you want to post your data. httppost.setParams(httpParameters); httppost.setEntity(reqEntity); httppost.addHeader("Accept", "text/html"); httppost.addHeader("Host", "www.xyz"); httppost.addHeader("User-Agent", "Android "); HttpResponse response1 = null; String errMessage = "Error"; try { response1 = httpclient.execute(httppost); final HttpEntity resEntity = response1.getEntity(); InputStream content = resEntity.getContent(); BufferedReader b = new BufferedReader(new InputStreamReader( content)); xml = XmlParser.getTrimmedResponse(b); if (response1 != null){ if(Integer.toString(response1.getStatusLine().getStatusCode()).equals("200")){ return "success"; } } } catch (Exception e) { e.printStackTrace(); errorstring=errorstring+e.getLocalizedMessage(); errMessage = "Network error"; return errMessage; } } else if(activeNetwork==null) { return "Available"; } } catch(Exception e) { Toast.makeText(getBaseContext(), "Network Connection not available", 1).show(); progressDialog.dismiss(); } } catch (Exception e) { errorstring=errorstring+e.getLocalizedMessage(); return "Network error"; } return "abc"; } protected void onPostExecute(String result) { //do your stuff } }

更多推荐

上传多张图片到服务器

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

发布评论

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

>www.elefans.com

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