从Android的上传文件到网络服务器 [英] Upload file from Android to webserver

查看:141
本文介绍了从Android的上传文件到网络服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是上传从Android的文件(影像)至Web服务器(我用codeingniter)的最佳方式。在谷歌我看到CustomMultipartEntity,Base64和等一些方法,但哪种方式更好?而我可以上传图像和JSON同时web服务器?

what is the best way for uploading files(image) from android to webserver(i use codeingniter). in google i saw some methods with CustomMultipartEntity, Base64 and etc, but which way is better? and can i upload an image and a json to webserver simultaneously?

推荐答案

是的,你可以将它们转换使用的Base64字符串上传图片。

Yes you can upload images by converting them using Base64 String.

我最喜欢的一张code的。

One of my favourite piece of code.

Android应用程序上传位置

我的COM preSS图像(重新大小)它只是为了节省侧(code底)

I compress the Image (Re size) it just to be save side (Code at the end)

Bitmap bitmap = resizeBitMapImage1(exsistingFileName, 800, 600);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,30, stream);
StrictMode.ThreadPolicy policy = new     StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image_data", Base64.encodeBytes(stream.toByteArray())));

// image_str = null;
stream.flush();
stream.close();
bitmap.recycle();
nameValuePairs.add(new BasicNameValuePair("FileName", FileName));

String url = "http://www.xyz.com/upload.aspx";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response1 = httpclient.execute(httppost);
Log.i("DataUploaderOffline_Image ","Status--> Completed");

ASPX页面code

 Response.ContentType = "text/plain";
 string c = Request.Form["image_data"];
 string FileName = Request.Form["FileName"];
 byte[] bytes = Convert.FromBase64String(c);

 System.Drawing.Image image;
 using (MemoryStream ms = new MemoryStream(bytes))
 {
      image = System.Drawing.Image.FromStream(ms);
      image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
      String Fname =   FileName + ".jpeg";
      image.Save(Server.MapPath("Image\\" + Fname), System.Drawing.Imaging.ImageFormat.Jpeg);
      Response.End();
 }

*的调整code *

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
        int targetHeight) {
    Bitmap bitMapImage = null;
    try {
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        double sampleSize = 0;
        Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                .abs(options.outWidth - targetWidth);
        if (options.outHeight * options.outWidth * 2 >= 1638) {
            sampleSize = scaleByHeight ? options.outHeight / targetHeight
                    : options.outWidth / targetWidth;
            sampleSize = (int) Math.pow(2d,
                    Math.floor(Math.log(sampleSize) / Math.log(2d)));
        }
        options.inJustDecodeBounds = false;
        options.inTempStorage = new byte[128];
        while (true) {
            try {
                options.inSampleSize = (int) sampleSize;
                bitMapImage = BitmapFactory.decodeFile(filePath, options);
                break;
            } catch (Exception ex) {
                try {
                    sampleSize = sampleSize * 2;
                } catch (Exception ex1) {

                }
            }
        }
    } catch (Exception ex) {

    }
    return bitMapImage;
}

这篇关于从Android的上传文件到网络服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆