package com.example.camera;


import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Locale;


import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {


private Button photograph;// 拍照
private Button upload;// 上传
private String PHOTO_FOLDER = new File(
Environment.getExternalStorageDirectory(), "").getPath()
+ "/myAndroid/";
private String PHOTO_NAME = "";
private String actionUrl = "http://11.206.128.150:8080/TestFileUploadService/upload.jsp";//上传路径
private static final int TIME_OUT = 10 * 1000; // 超时时间
private Handler handler;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setonlitener();
handler=new Handler(){
public void handleMessage(Message msg){
switch (msg.arg1) {  
           case 1:  
            upload.setText("上传成功");  
               break;  
           case 2:  
            upload.setText("上传失败"); 
           default:  
               break;  
           }  
}
};
}

public void init(){
File file = new File(PHOTO_FOLDER);
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
photograph = (Button) findViewById(R.id.photograph);
upload = (Button) findViewById(R.id.upload);

}



public void setonlitener(){


photograph.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

PHOTO_NAME = new DateFormat().format("yyyyMMdd_hhmmss",
Calendar.getInstance(Locale.CHINA)) + ".jpg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(PHOTO_FOLDER + PHOTO_NAME)));
startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);
}

});


upload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
uploadFile(new File(PHOTO_FOLDER + PHOTO_NAME));
}
}).start();
}
});
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.DEFAULT_KEYS_DIALER) {
try {
Bitmap take = resizeBitmap(getBitmapForFile(PHOTO_FOLDER+ PHOTO_NAME), 640);

File file = new File(PHOTO_FOLDER+ PHOTO_NAME);

BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);// 将图片压缩到流中
bos.flush();// 输出
bos.close();// 关闭

((ImageView) findViewById(R.id.imageView)).setImageBitmap(take);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float temp = ((float) height) / ((float) width);
int newHeight = (int) ((newWidth) * temp);
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
bitmap.recycle();
return resizedBitmap;
}


public static Bitmap getBitmapForFile(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
return bitmap;
}


public void uploadFile(File file) {
Message msgMessage=new Message();
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(actionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(TIME_OUT);
con.setConnectTimeout(TIME_OUT);
con.setDoInput(true);// 允许输入流
con.setDoOutput(true);// 允许输出流
con.setUseCaches(false);// 不允许使用缓存
con.setRequestMethod("POST");// 请求方式
con.setRequestProperty("Charset", "UTF-8");// 设置编码
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
if (file != null) {
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + PHOTO_NAME + "\""
+ end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(file);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
msgMessage.arg1=1;
/* 关闭DataOutputStream */
ds.close();
}
} catch (Exception e) {
msgMessage.arg1=2;
e.printStackTrace();
}finally{
handler.sendMessage(msgMessage);
}
}


@Override
protected void onDestroy() {
super.onDestroy();
File file = new File(PHOTO_FOLDER + PHOTO_NAME);
if (file.exists()) {
file.delete();
}
}

}


页面布局文件:

<?xml version="1.0" encoding="utf-8"?>
    android:id="@+id/screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
            android:id="@+id/logo_bg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        
                    android:layout_width="fill_parent"
            android:layout_height="370dp"/>

                    android:id="@+id/photograph"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           android:background="#ff0000"
            android:text="拍照" />
                    android:id="@+id/upload"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           android:background="#00ff00"
            android:text="上传" />




记得要加权限

   
   
   
   
   
   

   

更多相关文章

  1. android用于打开各种文件的intent
  2. Android base64 上传图片
  3. Delphi XE5 for android 调用Java类库必看的文件
  4. android带图片的AlertDialog和文件管理器(代码)

随机推荐

  1. Android面试经验二:
  2. 个人认为安卓开发前景
  3. android Uid 与 Pid 的区别与用法
  4. Android工程 引用另外一个Android工程(zz
  5. Android 里的Intent是什么意思
  6. Android:增强目录选择器对话框
  7. 在android下进行TDD开发
  8. Android性能测试 一些适用于Android Stud
  9. Android开发人暗讽阿里:抄得不到位 不懂
  10. Android中的签名验证(1)