首页 > 代码库 > android选择图片或拍照图片上传到服务器(包括上传参数)
android选择图片或拍照图片上传到服务器(包括上传参数)
在9ria论坛看到的,还没测试,先Mark与大家分享一下。 最近要搞一个项目,需要上传相册和拍照的图片,不负所望,终于完成了! 不过需要说明一下,其实网上很多教程拍照的图片,都是缩略图不是很清晰,所以需要在调用照相机的时候,事先生成一个地址,用于标识拍照的图片URI 具体上传代码:1.选择图片和上传界面,包括上传完成和异常的回调监听 |
package com.spring.sky.image.upload; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.spring.sky.image.upload.network.UploadUtil; import com.spring.sky.image.upload.network.UploadUtil.OnUploadProcessListener; /** * 说明:主要用于选择文件和上传文件操作 */ public class MainActivity extends Activity implements OnClickListener,OnUploadProcessListener{ private static final String TAG = "uploadImage"; /** * 去上传文件 */ protected static final int TO_UPLOAD_FILE = 1; /** * 上传文件响应 */ protected static final int UPLOAD_FILE_DONE = 2; // /** * 选择文件 */ public static final int TO_SELECT_PHOTO = 3; /** * 上传初始化 */ private static final int UPLOAD_INIT_PROCESS = 4; /** * 上传中 */ private static final int UPLOAD_IN_PROCESS = 5; /*** * 这里的这个URL是我服务器的javaEE环境URL */ private static String requestURL = "http://192.168.10.160:8080/fileUpload/p/file!upload"; private Button selectButton,uploadButton; private ImageView imageView; private TextView uploadImageResult; private ProgressBar progressBar; private String picPath = null; private ProgressDialog progressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } /** * 初始化数据 */ private void initView() { selectButton = (Button) this.findViewById(R.id.selectImage); uploadButton = (Button) this.findViewById(R.id.uploadImage); selectButton.setOnClickListener(this); uploadButton.setOnClickListener(this); imageView = (ImageView) this.findViewById(R.id.imageView); uploadImageResult = (TextView) findViewById(R.id.uploadImageResult); progressDialog = new ProgressDialog(this); progressBar = (ProgressBar) findViewById(R.id.progressBar1); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.selectImage: Intent intent = new Intent(this,SelectPicActivity.class); startActivityForResult(intent, TO_SELECT_PHOTO); break; case R.id.uploadImage: if(picPath!=null) { handler.sendEmptyMessage(TO_UPLOAD_FILE); }else{ Toast.makeText(this, "上传的文件路径出错", Toast.LENGTH_LONG).show(); } break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode==Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) { picPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH); Log.i(TAG, "最终选择的图片="+picPath); Bitmap bm = BitmapFactory.decodeFile(picPath); imageView.setImageBitmap(bm); } super.onActivityResult(requestCode, resultCode, data); } /** * 上传服务器响应回调 */ @Override public void onUploadDone(int responseCode, String message) { progressDialog.dismiss(); Message msg = Message.obtain(); msg.what = UPLOAD_FILE_DONE; msg.arg1 = responseCode; msg.obj = message; handler.sendMessage(msg); } private void toUploadFile() { uploadImageResult.setText("正在上传中..."); progressDialog.setMessage("正在上传文件..."); progressDialog.show(); String fileKey = "pic"; UploadUtil uploadUtil = UploadUtil.getInstance();; uploadUtil.setOnUploadProcessListener(this); //设置监听器监听上传状态 Map<String, String> params = new HashMap<String, String>(); params.put("orderId", "11111"); uploadUtil.uploadFile( picPath,fileKey, requestURL,params); } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case TO_UPLOAD_FILE: toUploadFile(); break; case UPLOAD_INIT_PROCESS: progressBar.setMax(msg.arg1); break; case UPLOAD_IN_PROCESS: progressBar.setProgress(msg.arg1); break; case UPLOAD_FILE_DONE: String result = "响应码:"+msg.arg1+"\n响应信息:"+msg.obj+"\n耗时:"+UploadUtil.getRequestTime()+"秒"; uploadImageResult.setText(result); break; default: break; } super.handleMessage(msg); } }; @Override public void onUploadProcess(int uploadSize) { Message msg = Message.obtain(); msg.what = UPLOAD_IN_PROCESS; msg.arg1 = uploadSize; handler.sendMessage(msg ); } @Override public void initUpload(int fileSize) { Message msg = Message.obtain(); msg.what = UPLOAD_INIT_PROCESS; msg.arg1 = fileSize; handler.sendMessage(msg ); } }
2.选择图片界面,主要涉及两种方式:选择图片和及时拍照图片
package com.spring.sky.image.upload; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; /** * @author spring sky<br> * 说明:主要用于选择文件操作 */ public class SelectPicActivity extends Activity implements OnClickListener{ /*** * 使用照相机拍照获取图片 */ public static final int SELECT_PIC_BY_TACK_PHOTO = 1; /*** * 使用相册中的图片 */ public static final int SELECT_PIC_BY_PICK_PHOTO = 2; /*** * 从Intent获取图片路径的KEY */ public static final String KEY_PHOTO_PATH = "photo_path"; private static final String TAG = "SelectPicActivity"; private LinearLayout dialogLayout; private Button takePhotoBtn,pickPhotoBtn,cancelBtn; /**获取到的图片路径*/ private String picPath; private Intent lastIntent ; private Uri photoUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.select_pic_layout); initView(); } /** * 初始化加载View */ private void initView() { dialogLayout = (LinearLayout) findViewById(R.id.dialog_layout); dialogLayout.setOnClickListener(this); takePhotoBtn = (Button) findViewById(R.id.btn_take_photo); takePhotoBtn.setOnClickListener(this); pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo); pickPhotoBtn.setOnClickListener(this); cancelBtn = (Button) findViewById(R.id.btn_cancel); cancelBtn.setOnClickListener(this); lastIntent = getIntent(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_layout: finish(); break; case R.id.btn_take_photo: takePhoto(); break; case R.id.btn_pick_photo: pickPhoto(); break; default: finish(); break; } } /** * 拍照获取图片 */ private void takePhoto() { //执行拍照前,应该先判断SD卡是否存在 String SDState = Environment.getExternalStorageState(); if(SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE" /*** * 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 * 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图 * 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰 */ ContentValues values = new ContentValues(); photoUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); /**-----------------*/ startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); }else{ Toast.makeText(this,"内存卡不存在", Toast.LENGTH_LONG).show(); } } /*** * 从相册中取图片 */ private void pickPhoto() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return super.onTouchEvent(event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { doPhoto(requestCode,data); } super.onActivityResult(requestCode, resultCode, data); } /** * 选择图片后,获取图片的路径 * @param requestCode * @param data */ private void doPhoto(int requestCode,Intent data) { if(requestCode == SELECT_PIC_BY_PICK_PHOTO ) //从相册取图片,有些手机有异常情况,请注意 { if(data =http://www.mamicode.com/= null)>
3. 上传工具类,主要实现了图片的上传,上传过程的初始化监听和上传完成的监听,还有上传耗时的计算package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.Map; import java.util.UUID; import android.util.Log; /** * * 上传工具类 * 支持上传文件和参数 */ public class UploadUtil { private static UploadUtil uploadUtil; private static final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成 private static final String PREFIX = "--"; private static final String LINE_END = "\r\n"; private static final String CONTENT_TYPE = "multipart/form-data"; // 内容类型 private UploadUtil() { } /** * 单例模式获取上传工具类 * @return */ public static UploadUtil getInstance() { if (null == uploadUtil) { uploadUtil = new UploadUtil(); } return uploadUtil; } private static final String TAG = "UploadUtil"; private int readTimeOut = 10 * 1000; // 读取超时 private int connectTimeout = 10 * 1000; // 超时时间 /*** * 请求使用多长时间 */ private static int requestTime = 0; private static final String CHARSET = "utf-8"; // 设置编码 /*** * 上传成功 */ public static final int UPLOAD_SUCCESS_CODE = 1; /** * 文件不存在 */ public static final int UPLOAD_FILE_NOT_EXISTS_CODE = 2; /** * 服务器出错 */ public static final int UPLOAD_SERVER_ERROR_CODE = 3; protected static final int WHAT_TO_UPLOAD = 1; protected static final int WHAT_UPLOAD_DONE = 2; /** * android上传文件到服务器 * * @param filePath * 需要上传的文件的路径 * @param fileKey * 在网页上<input type=file name=xxx/> xxx就是这里的fileKey * @param RequestURL * 请求的URL */ public void uploadFile(String filePath, String fileKey, String RequestURL, Map<String, String> param) { if (filePath == null) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); return; } try { File file = new File(filePath); uploadFile(file, fileKey, RequestURL, param); } catch (Exception e) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); e.printStackTrace(); return; } } /** * android上传文件到服务器 * * @param file * 需要上传的文件 * @param fileKey * 在网页上<input type=file name=xxx/> xxx就是这里的fileKey * @param RequestURL * 请求的URL */ public void uploadFile(final File file, final String fileKey, final String RequestURL, final Map<String, String> param) { if (file == null || (!file.exists())) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); return; } Log.i(TAG, "请求的URL=" + RequestURL); Log.i(TAG, "请求的fileName=" + file.getName()); Log.i(TAG, "请求的fileKey=" + fileKey); new Thread(new Runnable() { //开启线程上传文件 @Override public void run() { toUploadFile(file, fileKey, RequestURL, param); } }).start(); } private void toUploadFile(File file, String fileKey, String RequestURL, Map<String, String> param) { String result = null; requestTime= 0; long requestTime = System.currentTimeMillis(); long responseTime = 0; try { URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(readTimeOut); conn.setConnectTimeout(connectTimeout); conn.setDoInput(true); // 允许输入流 conn.setDoOutput(true); // 允许输出流 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); // 请求方式 conn.setRequestProperty("Charset", CHARSET); // 设置编码 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); // conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); /** * 当文件不为空,把文件包装并且上传 */ DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); StringBuffer sb = null; String params = ""; /*** * 以下是用于上传参数 */ if (param != null && param.size() > 0) { Iterator<String> it = param.keySet().iterator(); while (it.hasNext()) { sb = null; sb = new StringBuffer(); String key = it.next(); String value = http://www.mamicode.com/param.get(key);>android选择图片或拍照图片上传到服务器(包括上传参数)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。