代码改变世界

Android开发之将拍摄的图片传至服务器

2014-01-06 14:29  y-z-f  阅读(1635)  评论(1编辑  收藏  举报
  1 package com.example.oldtab;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.File;
  5 import java.io.IOException;
  6 import java.io.OutputStream;
  7 import java.net.HttpURLConnection;
  8 import java.net.MalformedURLException;
  9 import java.net.URL;
 10 
 11 import android.content.Intent;
 12 import android.graphics.Bitmap;
 13 import android.graphics.BitmapFactory;
 14 import android.net.Uri;
 15 import android.os.Bundle;
 16 import android.os.Environment;
 17 import android.provider.MediaStore;
 18 import android.support.v7.app.ActionBarActivity;
 19 import android.view.View;
 20 import android.view.View.OnClickListener;
 21 import android.widget.Button;
 22 import android.widget.ImageView;
 23 
 24 public class Group extends ActionBarActivity {
 25     private static final int TAKE_PICTURE = 0x0;
 26 
 27     private static final String TAG_SEP = "----ninesofttestpostfile";
 28 
 29     private ImageView imageview;
 30     private Button button;
 31     private Uri outputFileUri;
 32     private Bitmap bitmap;
 33     private File file;
 34 
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38         setContentView(R.layout.activity_group);
 39 
 40         imageview = (ImageView) findViewById(R.id.imageView1);
 41         button = (Button) findViewById(R.id.button1);
 42 
 43         button.setOnClickListener(new OnClickListener() {
 44 
 45             @Override
 46             public void onClick(View v) {
 47                 File file = new File(Environment.getExternalStorageDirectory(),
 48                         "test.jpg");
 49                 outputFileUri = Uri.fromFile(file);
 50 
 51                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 52                 intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
 53 
 54                 startActivityForResult(intent, TAKE_PICTURE);
 55             }
 56 
 57         });
 58 
 59     }
 60 
 61     @Override
 62     protected void onDestroy() {
 63         if (bitmap != null && !bitmap.isRecycled()) {
 64             bitmap.recycle();
 65         }
 66         super.onDestroy();
 67     }
 68 
 69     @Override
 70     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 71         if (requestCode == TAKE_PICTURE) {
 72             if (data == null) {
 73                 if (bitmap != null) {
 74                     if (!bitmap.isRecycled()) {
 75                         bitmap.recycle();
 76                     }
 77                 }
 78                 int width = 1024;
 79                 int height = 768;
 80 
 81                 file = new File(Environment.getExternalStorageDirectory(),
 82                         "test.jpg");
 83 
 84                 outputFileUri = Uri.fromFile(file);
 85 
 86                 BitmapFactory.Options factoryoption = new BitmapFactory.Options();
 87                 factoryoption.inJustDecodeBounds = true;
 88 
 89                 BitmapFactory
 90                         .decodeFile(outputFileUri.getPath(), factoryoption);
 91 
 92                 int outwidth = factoryoption.outWidth;
 93                 int outheight = factoryoption.outHeight;
 94 
 95                 int scale = Math.min(outwidth / width, outheight / height);
 96 
 97                 factoryoption.inJustDecodeBounds = false;
 98                 factoryoption.inSampleSize = scale;
 99                 factoryoption.inPurgeable = true;
100 
101                 bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),
102                         factoryoption);
103                 imageview.setImageBitmap(bitmap);
104 
105                 saveToService();
106             }
107         }
108         super.onActivityResult(requestCode, resultCode, data);
109     }
110 
111     private void saveToService() {
112         new Thread(new Runnable() {
113 
114             @Override
115             public void run() {
116                 try {
117                     URL url = new URL("http://172.16.101.79/PostFile.ashx");
118                     HttpURLConnection conn = (HttpURLConnection) url
119                             .openConnection();
120                     conn.setRequestMethod("POST");
121                     conn.setRequestProperty("Connection", "keep-alive");
122                     conn.setRequestProperty("Accept", "*/*");
123                     conn.setRequestProperty("Content-Type",
124                             "multipart/form-data;boundary=" + TAG_SEP);
125                     conn.setDoOutput(true);
126                     OutputStream out = conn.getOutputStream();
127                     out.write(("--" + TAG_SEP + "\r\n").getBytes());
128                     out.write(("Content-Disposition: form-data; name=\"postfile\"; filename=\"test.jpg\"\r\n")
129                             .getBytes());
130                     out.write(("Content-Type:image/jpeg\r\n\r\n").getBytes());
131                     ByteArrayOutputStream fr = new ByteArrayOutputStream();
132                     bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fr);
133                     out.write(fr.toByteArray());
134                     out.write(("\r\n\r\n--" + TAG_SEP + "--\r\n").getBytes());
135                     out.flush();
136                     out.close();
137                     conn.getResponseCode();
138                     fr.close();
139 
140                 } catch (MalformedURLException e) {
141                     e.printStackTrace();
142                 } catch (IOException e) {
143                     e.printStackTrace();
144                 }
145             }
146 
147         }).start();
148 
149     }
150 }

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".Group" >
 6 
 7     <ImageView
 8         android:id="@+id/imageView1"
 9         android:layout_width="500dp"
10         android:layout_height="300dp" />
11 
12     <Button
13         android:id="@+id/button1"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_below="@+id/imageView1"
17         android:layout_centerHorizontal="true"
18         android:layout_marginTop="50dp"
19         android:text="Button" />
20 
21 </RelativeLayout>