Android实现图片(拍照+相册)上传功能

来源:互联网 发布:用友软件银川经销处 编辑:程序博客网 时间:2024/04/28 22:27

今天给大家讲一下如何实现图片上传功能,以上所讲呢全是经验之谈,把工作中的代码以功能的形式展示出来,即为了大家学习借鉴,也为了日后工作的方便,加深理解。
图片上传,众所周知,无非是是拍照和相册选取两种,可以用dialog,activity,popwindow等展示界面,具体大家自己去实现,本文只讲功能

调取拍照功能:

    private void camera() {        try {            tempImg1 = new File(MainActivity.this.getExternalCacheDir(), System.currentTimeMillis() + ".jpg");            if (!tempImg1.exists()) {                boolean b = tempImg1.createNewFile();                if (b) {                    log.i(tempImg1.getAbsolutePath());                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempImg1));                    startActivityForResult(intent, 1);                }            }        } catch (IOException e) {            log.e(e);        }    }

调取相册功能:

private void photos() {        Intent getImage = new Intent(Intent.ACTION_PICK, null);        getImage.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//这是图片类型        startActivityForResult(getImage, 2);    }

然后是如何在 public void onActivityResult(int requestCode, int resultCode, Intent data)里进行处理呢?

requestCode请求码,resultCode结果码,data是传递的数据

  @Override    public void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == Activity.RESULT_OK) {            switch (requestCode) {                case 1:                    if (tempImg1 != null) {                        //调用裁剪功能或者压缩                        //scaleImage(Uri.fromFile(tempImg1));                    }                    break;                case 2:                    if (data != null) {                        //调用裁剪功能或者压缩                       // scaleImage(data.getData());                    }                    break;                default:                    break;            }        }    }

裁剪有系统的裁剪方式,也有自定义的裁剪方式
系统裁剪:

    /**     * 裁剪图片     */    public void startPhotoZoom(Uri uri, int size) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面.        intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例.        intent.putExtra("aspectY", 1);// x:y=1:1        intent.putExtra("outputX", size);//图片输出大小        intent.putExtra("outputY", size);        intent.putExtra("output", Uri.fromFile(tempImg));        intent.putExtra("outputFormat", "jpg");// 返回格式        startActivityForResult(intent, 3);    }

自定裁剪很多,这里也为大家推荐github上比较流行的几款裁剪库

https://github.com/wangchang163/uCrop
这里写图片描述
https://github.com/wangchang163/cropper
这里写图片描述
https://github.com/wangchang163/android-crop
这里写图片描述
以上都是很经典的老牌crop库,大家可以作为参考!
裁剪过后自然是保存图片,进行上传啦,上传具体参考后台接口,不做介绍了,当然还有一种情况,就是不需要裁剪,只进行压缩,因为裁剪是选取局部图片,当图片太大,又不需要裁剪时,就需要进行压缩了,这里提供一个压缩方法

private void scaleImage(Uri uri) {        Bitmap bitmap = null;        try {            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);            int width = bitmap.getWidth();            int height = bitmap.getHeight();            if (width > 800 || height > 800) {                if (width > height) {                    float scaleRate = (float) (800.0 / width);                    width = 800;                    height = (int) (height * scaleRate);                    Bitmap map = Bitmap.createScaledBitmap(bitmap, width, height, true);                    saveBitmap(map);                } else {                    float scaleRate = (float) (800.0 / height);                    height = 800;                    width = (int) (width * scaleRate);                    Bitmap map = Bitmap.createScaledBitmap(bitmap, width, height, true);                    saveBitmap(map);                }            } else {                saveBitmap(bitmap);            }        } catch (IOException e) {            e.printStackTrace();        }    }    private void saveBitmap(Bitmap map) {        File file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg");        try {            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));            map.compress(Bitmap.CompressFormat.JPEG, 80, bos);            bos.flush();            bos.close();            updateImg(file);//上传图片接口        } catch (Exception e) {            e.printStackTrace();        }    }

嗯,就这样吧,基本上可以实现此功能,仅供参考借鉴,如有帮助,请点赞,谢谢!

1 0
原创粉丝点击