Android中的图片上传问题
昨天做项目遇到上传图片的问题,服务端提供的接口如下,
1 /*** 2 * 上传文件 3 * @param file 文件对象 4 * @param filename 重命名 5 * @return uuid 服务器文件的唯一标识,之后用于下载 6 * @throws HttpException 7 * @throws IOException 8 */ 9 public static String upload(File file, String filename) throws HttpException, IOException { 10 HttpClient client = new HttpClient(); 11 12 PostMethod postMethod = new PostMethod(UploadUrl); 13 System.out.println("upload res " + filename); 14 if(null == filename || "".equals(filename) || file == null) { 15 return null; 16 } 17 Part[] p = new Part[2]; 18 p[0] = new StringPart("name", filename); 19 p[1] = new FilePart("upload", filename, file); 20 MultipartRequestEntity mrp = new MultipartRequestEntity(p , postMethod.getParams()); 21 22 postMethod.setRequestEntity(mrp); 23 client.executeMethod(postMethod); 24 25 String res = postMethod.getResponseBodyAsString(); 26 Pattern P = Pattern.compile("\"uuid\":\"(.*)\""); 27 Matcher m = P.matcher(res); 28 29 String uuid; 30 if(m.find() == true) { 31 uuid = m.group(1); 32 } else { 33 uuid = null; 34 } 35 36 return uuid; 37 }
可以通过调用照相机和图库上传图片,这就遇到两个问题,图片过大导致imageview.setImageBitmap(bitmap);时程序就崩溃。
我的解决办法是,把图片另存到一个目录(类似于复制一份,或者生成一个缩略图,然后上传这张图片,每次重新上传时会吧就的图片覆盖,这样不会占用户太大空间)
代码如下
1 picfromdoc.setOnClickListener(new OnClickListener() {//从图库找图片 2 public void onClick(View v) { 3 Intent intent = new Intent(Intent.ACTION_PICK, null); 4 intent.setDataAndType( 5 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); 6 startActivityForResult(intent, 0); 7 } 8 }); 9 picfromcam.setOnClickListener(new OnClickListener() {//调用照相机 10 11 public void onClick(View v) { 12 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 13 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( 14 Environment.getExternalStorageDirectory(), "jucanbao.jpg"))); 15 startActivityForResult(intent, 1); 16 } 17 });
1 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 2 if (requestCode == 1) { 3 // 设置文件保存路径这里放在跟目录下 4 Bitmap bitmap = BitmapFactory.decodeFile(Environment 5 .getExternalStorageDirectory().getPath() 6 + "/" 7 + "jucanbao.jpg"); 8 float wight = bitmap.getWidth(); 9 float height = bitmap.getHeight(); 10 bitmap = ZoomBitmap.zoomImage(upbitmap, wight / 5, height / 5); 11 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 12 bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream); 13 saveMyBitmap("jucanbao", bitmap); 14 imageview.setImageBitmap(bitmap); 15 } 16 17 if (requestCode == 0) { 18 if (data != null) { 19 Bitmap bitmap = BitmapFactory 20 .decodeFile(getAbsoluteImagePath(data.getData()));// 取相机图片的路径 21 float wight = bitmap.getWidth(); 22 float height = bitmap.getHeight(); 23 bitmap = ZoomBitmap.zoomImage(bitmap, wight / 5, height / 5); 24 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 25 bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream); 26 saveMyBitmap("jucanbao", bitmap); 27 imageview.setImageBitmap(bitmap); 28 } 29 } 30 super.onActivityResult(requestCode, resultCode, data); 31 }
取相机图片绝对路径
1 protected String getAbsoluteImagePath(Uri uri) { 2 String[] proj = { MediaStore.Images.Media.DATA }; 3 Cursor cursor = managedQuery(uri, proj, 4 null, 5 null, 6 null); 7 8 int column_index = cursor 9 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 10 cursor.moveToFirst(); 11 return cursor.getString(column_index); 12 }
缩小图片
1 public static class ZoomBitmap { 2 3 public static Bitmap zoomImage(Bitmap bgimage, double newWidth, 4 double newHeight) { 5 // 获取这个图片的宽和高 6 float width = bgimage.getWidth(); 7 float height = bgimage.getHeight(); 8 // 创建操作图片用的matrix对象 9 Matrix matrix = new Matrix(); 10 // 计算宽高缩放率 11 float scaleWidth = ((float) newWidth) / width; 12 float scaleHeight = ((float) newHeight) / height; 13 // 缩放图片动作 14 matrix.postScale(scaleWidth, scaleHeight); 15 Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, 16 (int) height, matrix, true); 17 return bitmap; 18 } 19 }
保存图片
1 public static void saveMyBitmap(String id, Bitmap mBitmap) {// 生成图片 2 File f = new File("/sdcard/" + id + ".jpg"); 3 try { 4 f.createNewFile(); 5 6 } catch (IOException e) { 7 } 8 FileOutputStream fOut = null; 9 try { 10 fOut = new FileOutputStream(f); 11 } catch (FileNotFoundException e) { 12 e.printStackTrace(); 13 } 14 15 mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 16 try { 17 fOut.flush(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 try { 22 fOut.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 }
最后调用文件上传的代码,将重新生成的图片上传即可。