Android实现

调用系统相机,拍照:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            getFileUri();            intent.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);            startActivityForResult(intent, CODE_CAMERA);  private void getFileUri() {    image_name = Calendar.getInstance().getTimeInMillis() + ".jpg";    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + image_name);    file_uri = Uri.fromFile(file);}

在onActivityResult里面接收图片并Base64处理:

  @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == CODE_CAMERA && resultCode == RESULT_OK) {        new EncodeImage().execute();   //把bitmap转换成base64字符串    }}

EncodeImage是一个AsyncTask,doInBackground里面从uri里面获取bitmap,然后转入输出流,最终转换为base64编码字符串:

  @Override    protected Void doInBackground(Void... voids) {        bitmap = BitmapFactory.decodeFile(file_uri.getPath());        ByteArrayOutputStream stream = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);        byte[] array = stream.toByteArray();        encoded_string = Base64.encodeToString(array, 0);        bitmap.recycle();  //防止oom        return null;    }

然后就可以上传到服务器了:

    private void uploadImage() {    HashMap<String, String> map = new HashMap<>();    map.put("encoding_string", encoded_string);    map.put("image_name", image_name);    OkHttpUtils.post()            .url("http:192.168.0.112/phpdemo/uploadimage.php")            .params(map)            .tag(this)            .build()            .execute(new StringCallback() {                @Override                public void onError(Call call, Exception e, int id) {                    Log.e("出错了", "错误信息:" + e.getMessage());                }                @Override                public void onResponse(String response, int id) {                    Log.e("成功or失败", "信息:" + response);                }            });}

在上传服务器过程中,遇到两个问题,第一,提示POST Content-Length of ... bytes exceeds the limit of 8388608 bytes,这个错误是因为php默认最大post上传8M,更改php.ini里面的post_max_size=1000M就ok了;第二,当第二次拍照的时候会出现OOM的情况,检查代码发现bitmap没有recycle。

OVER

更多相关文章

  1. Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
  2. Android(安卓)WebView相关属性
  3. android照相及照片上传
  4. 2010.11.28(3)———android AlertDialog:android.view.WindowMan
  5. android中json解析及使用(中)
  6. android拍照造成内存泄露问题
  7. Android(安卓)操作SQLite数据库之一
  8. Android(安卓)Camera 使用小结
  9. Android(安卓)Camera 使用小结

随机推荐

  1. 面试常问,你看源码学到了什么?
  2. 推荐给初级 Java 程序员的 3 本进阶书
  3. 构造模式实践
  4. 原理暂且不谈,定时器你当真会用?
  5. 编码不规范,同事真的会两行泪?
  6. 面试官问我,SpringApplication.run做了哪
  7. 自学第四十八天
  8. 谁在关心toString的性能?
  9. Monadic Function_Haskell笔记12
  10. 从JDK中,我们能学到哪些设计模式?