Android 图片选择和拍照(剪辑)及压缩问题 工具类

图片选择和拍照在开发过程中,会遇到不少坑:

1.版本4.4以后选择图片后不会返回绝对路径,但返回Uri,要再查询一次

2.去剪辑时,设置了true的话直接返回bitmap,可能会很占内存,有些机子会挂掉(OOM)或者不会返回

3.图片未更好的压缩,应该做到宽高比压缩后再质量压缩,下面例子可以把几MB的图片压缩到100kb左右

4.解决7.0版本文件访问问题

下面代码处理解决了上面的问题,是个小笔记,以后就不会再麻烦找“轮子”了。

1.在AndroidManifest添加FileProvider

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

2.在res文件夹下添加文件夹xml,再添加file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path
        name="root"
        path="" />
    <!--<files-path-->
        <!--name="files"-->
        <!--path="" />-->
    <!--<cache-path-->
        <!--name="cache"-->
        <!--path="" />-->
    <!-- <external-path-->
         <!--name="external"-->
         <!--path="" />-->
    <!--<external-path-->
        <!--name="external"-->
        <!--path="/apk" />-->
    <external-path
        name="external"
        path="" />
    <!--<external-files-path-->
        <!--name="name"-->
        <!--path="path" />-->
    <!--<external-cache-path-->
        <!--name="name"-->
        <!--path="path" />-->
</paths>

<!--<root-path/> 代表设备的根目录new File("/");-->
<!--<files-path/> 代表context.getFilesDir()-->
<!--<cache-path/> 代表context.getCacheDir()-->
<!--<external-path/> 代表Environment.getExternalStorageDirectory()-->
<!--<external-files-path>代表context.getExternalFilesDirs()-->
<!--<external-cache-path>代表getExternalCacheDirs()-->

3.使用:

调用SelectPicUtil.getByAlbum(Activity.this); 选择相册

调用SelectPicUtil.getByCamera(Activity.this);进行拍照

然后在onActivityResult进行设置返回:

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri uri = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data);
        //下面是需求要裁剪的示例
//        Uri uri = SelectPicUtil.onActivityResult(this, requestCode, resultCode, data,1,1, 500,500, true);
        if (null != uri) { // 当不为空时获取到图片Uri
            decodeUriBitmap(uri);
        }
    }


    private void decodeUriBitmap(final Uri uri) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    String picturePath = SelectPicUtil.getImageAbsolutePath(OtherMsgActivity.this, uri);
                    if (!TextUtils.isEmpty(picturePath)) {
                        Bitmap bitmapSrc = SelectPicUtil.getImageThumbnail(picturePath, 1000, 1000); // 比例压缩图片
                        if (bitmapSrc != null) {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            bitmapSrc.compress(Bitmap.CompressFormat.JPEG, 80, baos); // 图片质量压缩80%
                            bitmapSrc.recycle();
                            byte[] bytes = baos.toByteArray();
                            try {
                                if (null != baos) {
                                    baos.close();
                                }
                            } catch (IOException e) {
                            }
                            // 对字节数组Base64编码
                            final String bitmap = Base64.encodeToString(bytes, Base64.DEFAULT);// 返回Base64编码过的字节数组字符串
                            upLoadPic(bitmap);
                        } else {
                            upLoadPic("");
                        }
                    } else {
                        upLoadPic("");
                    }

                } catch (Exception e) {
                }

            }
        }).start();

    }

4.工具类:

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.yingyou.demo.BuildConfig;

import java.io.File;

public class SelectPicUtil {

    private static final String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.jpg";

    public static final int GET_BY_ALBUM = 801;// 打开相册
    public static final int GET_BY_CAMERA = 802;// 打开相机
    public static final int CROP = 803;// 裁剪图片

    public static void getByAlbum(Activity act) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            act.startActivityForResult(intent, GET_BY_ALBUM);
        } else {
            Intent intentFromGallery = new Intent();
            intentFromGallery.setType("image/*");
            intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
            act.startActivityForResult(intentFromGallery, GET_BY_ALBUM);
        }
    }

    public static void getByCamera(Activity act) {
        getByCamera(act, temp, GET_BY_CAMERA);
    }

    // 给其他地方用
    public static void getByCamera(Activity act, String path, int requestCode) {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Uri uri = getUri(act, new File(path));
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            takePictureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
            act.startActivityForResult(takePictureIntent, requestCode);
        } else {
            ToastUtils.showText("请安装sd卡");
        }
    }

    public static Uri onActivityResult(Activity act, int requestCode,
                                       int resultCode, Intent data) {
        return onActivityResult(act, requestCode, resultCode, data, 0, 0, 0, 0, false);
    }

    public static Uri onActivityResult(Activity act, int requestCode,
                                       int resultCode, Intent data, int w, int h, int aspectX, int aspectY) {
        return onActivityResult(act, requestCode, resultCode, data, w, h, aspectX, aspectY, true);
    }

    public static Uri onActivityResult(Activity act, int requestCode,
                                       int resultCode, Intent data, int outputX, int outputY, int aspectX, int aspectY, boolean isCut) {
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = null;
            switch (requestCode) {
                case GET_BY_ALBUM:
                    if (isCut) {
                        if (null == data) return null;
                        String path = getImageAbsolutePath(act, data.getData());
                        if (TextUtils.isEmpty(path)) return null;
                        uri = getUri(act, new File(path));
                    } else {
                        return data.getData();
                    }
                    break;
                case GET_BY_CAMERA:
                    if (isCut) {
                        uri = getUri(act, new File(temp));
                    } else {
                        return Uri.parse(temp);
                    }
                    break;
                case CROP:
//                    return getUri(act, new File(temp));
                    return Uri.fromFile(new File(temp));
            }
            if (isCut && null != uri) {
                crop(act, uri, outputX, outputY, aspectX, aspectY);
                return null;
            }
        }
        return null;
    }

    public static void crop(Activity act, Uri uri, int outputX, int outputY, int aspectX, int aspectY) {
        if (outputX == 0 && outputY == 0) {
            outputX = outputY = 480;
        }
        if (aspectX == 0 && aspectY == 0) {
            aspectX = aspectY = 1;
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);

//        intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri(act, new File(temp)));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(temp)));

        intent.putExtra("outputFormat", "JPEG");

        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", false);
        act.startActivityForResult(intent, CROP);
    }

    public static String getImageAbsolutePath(Activity context, Uri uri) {
        if (null == uri)
            return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null,
                    null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }


    public static Uri getUri(Context context, File file) {
        Uri fileUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			// BuildConfig 是工程包路径下的:如:com.yingyou.demo.BuildConfig
            fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
        } else {
            fileUri = Uri.fromFile(file);
        }

        return fileUri;
    }
	
	   public static Bitmap getImageThumbnail(String imagePath, int width,
                                           int height) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        // Options中有个属性inJustDecodeBounds。我们可以充分利用它,来避免大图片的溢出问题
        options.inJustDecodeBounds = true;// 设置为true可以不加载到内存,直接获取Bitmap宽高
        // 获取这个图片的宽和高,注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        if (bitmap == null) {
            // 计算缩放比
            int h = options.outHeight;// 获取Bitmap的实际高度
            int w = options.outWidth;// 获取Bitmap的实际宽度

            int beWidth = w / width;
            int beHeight = h / height;
            int rate = 1;
            if (beWidth < beHeight) {
                rate = beWidth;
            } else {
                rate = beHeight;
            }
            if (rate <= 0) {// 图片实际大小小于缩略图,不缩放
                rate = 1;
            }
            options.inSampleSize = rate;// rate就是压缩的比例
            options.inJustDecodeBounds = false;
            // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(imagePath, options);// 获取压缩后的图片
        }
        return bitmap;
    }

    public static Bitmap getBitmapFormUri(Activity ac, Uri uri) {
        InputStream input;
        try {
            input = ac.getContentResolver().openInputStream(uri);
            BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
            onlyBoundsOptions.inJustDecodeBounds = true;
            onlyBoundsOptions.inDither = true;// optional
            onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
            BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
            input.close();
            int originalWidth = onlyBoundsOptions.outWidth;
            int originalHeight = onlyBoundsOptions.outHeight;
            if ((originalWidth == -1) || (originalHeight == -1))
                return null;
            // 图片分辨率以480x800为标准
            float hh = 800f;// 这里设置高度为800f
            float ww = 480f;// 这里设置宽度为480f
            // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
            int be = 1;// be=1表示不缩放
            if (originalWidth > originalHeight && originalWidth > ww) {// 如果宽度大的话根据宽度固定大小缩放
                be = (int) (originalWidth / ww);
            } else if (originalWidth < originalHeight && originalHeight > hh) {// 如果高度高的话根据宽度固定大小缩放
                be = (int) (originalHeight / hh);
            }
            if (be <= 0)
                be = 1;
            // 比例压缩
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = be;// 设置缩放比例
            bitmapOptions.inDither = true;// optional
            bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
            input = ac.getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(input, null,
                    bitmapOptions);
            input.close();
            return compressImage(bitmap);// 再进行质量压缩
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值