2013-05-24 2 views
0

파일 (이미지)을 Android 서버에서 웹 서버로 업로드하는 가장 좋은 방법은 무엇입니까 (codeingniter 사용). 구글에서 내가 CustomMultipartEntity, Base64 등 몇 가지 방법을 봤어,하지만 어떤 방법이 더 낫지? 동시에 이미지와 json을 웹 서버에 동시에 업로드 할 수 있습니까?파일을 Android에서 웹 서버로 업로드

답변

1

예, Base64 String을 사용하여 이미지를 변환하여 업로드 할 수 있습니다.

내가 가장 좋아하는 코드 중 하나입니다.

안드로이드 앱 업로드 위치

나는 이미지 (재 크기) 그냥 (끝 코드) 측면을 절약 할 수

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 페이지 코드

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(); 
} 
를 압축

* 크기 조정 코드 *

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; 
} 
관련 문제