Android文件操作(读写,压缩,转换)工具类

package com.test.demo.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import com.test.demo.application.Application;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
public class ImageDispose {
    private static final int zoomBitmapH = 240;
    private static final int zoomBitmapW = 240;
    private static final int zoomBitmapFH = 160;
    private static final int zoomBitmapFW = 160;
    private static final int zoomBitmapH2 = 280;
    private static final int zoomBitmapW2 = 240;
    private static final int zoomBitmapFH2 = 187;
    private static final int zoomBitmapFW2 = 160;
    private static final long bitmapMaxSize = 30720;
    //    private static final String outputFile = Environment.getExternalStorageDirectory() + "/TEST/icon/";
//    private static final String outputFile2 = Environment.getExternalStorageDirectory() + "/TEST/";
    private static final String outputFile = Application.getApplication().getExternalFilesDir(null).getAbsolutePath() + "/AVATAR";
    private static final String outputFile2 = Application.getApplication().getExternalFilesDir(null).getAbsolutePath() + "/PIC/";
    private static final String outputPicName = "crop_photo.jpg";
    private static final String outputPicName0 = "preview.png";
    private static final String outputPicName1 = "bg.png";
    private static final String outputPicName2 = "pic.data";
    private static final String outputPicName3 = "new.hex";
    private static String image_type = "";
    public static final String IMAGE_PNG = "PNG";
    public static final String IMAGE_JPG = "JPG";
    public static void setImageType(String type) {
        image_type = type;
     * 将drawable转化为Bytes
     * @return
    public static byte[] getByteFromDrawable(Context context, int image_id) {
        Bitmap bit = getBitmapFromDrawable(context, image_id);
        LogUtils.d("ts", "------ 1 ------ bit : " + bit.getAllocationByteCount());
        return getByteFromBit(bit);
     * 将drawable转化为Bytes
     * @return
    public static byte[] getByteFromDrawable2(Context context, int image_id) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bit = getBitmapFromDrawable(context, image_id);
        LogUtils.d("ts", "------ 1 ------ bit : " + bit.getAllocationByteCount());
        bit.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
//        return getByteFromBit2(bit);
     * Drawable转换成一个Bitmap
     * @param drawable drawable对象
     * @return
    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
     * 将Bitmap转化为Bytes
     * @param bm
     * @return
    public static byte[] getByteFromBit(Bitmap bm) {
        if (bm != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            LogUtils.d("ts", "ImageDispose getByteFromBit options = 100" + " ; length : " + baos.toByteArray().length);
//            if(image_type.equals(IMAGE_PNG)){
//                bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
//                return baos.toByteArray();
//            }
            int options = 90;
            while (baos.toByteArray().length > bitmapMaxSize) {
                // 重置baos即清空baos
                baos.reset();
                // 这里压缩options%,把压缩后的数据存放到baos中
                bm.compress(Bitmap.CompressFormat.JPEG, options, baos);
                LogUtils.d("ts", "ImageDispose getByteFromBit JPG options = " + options + " ; length : " + baos.toByteArray().length);
                if (options == 1) {
                    break;
                } else if (options <= 10) {
                    options -= 1;
                } else {
                    options -= 10;
            return baos.toByteArray();
        return null;
     * 将Bitmap转化为Bytes
     * @param
     * @return
    public static byte[] getByteFromBit2(Bitmap bm) {
        if (bm != null) {
//            Bitmap bm = compressImage(b);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
            int options = 90;
            while (baos.toByteArray().length > bitmapMaxSize) {
                // 重置baos即清空baos
                baos.reset();
                // 这里压缩options%,把压缩后的数据存放到baos中
                bm.compress(Bitmap.CompressFormat.PNG, options, baos);
                LogUtils.d("ts", "ImageDispose getByteFromBit PNG options = " + options + " ; length : " + baos.toByteArray().length);
                if (options == 1) {
                    break;
                } else if (options <= 10) {
                    options -= 1;
                } else {
                    options -= 10;
            LogUtils.i("ts", "PNG_压缩后图片的大小" +
                    (bm.getByteCount() / 1024) +
                    "KB宽度为" + bm.getWidth() +
                    "高度为" + bm.getHeight() + "byte[]:" + baos.toByteArray().length);
            return baos.toByteArray();
        return null;
     * 质量压缩方法
     * @param image
     * @return
    public static Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 90;
        while (baos.toByteArray().length > bitmapMaxSize) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset(); // 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
            if (options == 1) {
                break;
            } else if (options <= 10) {
                options -= 1;
            } else {
                options -= 10;// 每次都减少10
//        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
//        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
        Bitmap bitmap = getBitmapFromBytes(baos.toByteArray(), null);
        LogUtils.i("ts", "质量压缩后图片的大小" +
                (bitmap.getByteCount() / 1024) +
                "KB宽度为" + bitmap.getWidth() +
                "高度为" + bitmap.getHeight());
        return bitmap;
     * 采样率压缩
     * @param bitmap
     * @param sampleSize 采样率为2的整数倍,非整数倍四舍五入,如4的话,就是原图的1/4
     * @return 尺寸变化
    public static Bitmap getBitmap(Bitmap bitmap, int sampleSize) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = sampleSize;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
        LogUtils.i("ts", "采样率压缩后图片的大小" +
                (bit.getByteCount() / 1024) +
                "KB宽度为" + bit.getWidth() +
                "高度为" + bit.getHeight());
        return bit;
     * bitmap bg 缩放
     * 指定大小
     * @param bitmap 对象
     * @return newBmp 新 Bitmap对象
    public static Bitmap zoomBitmap(Bitmap bitmap) {
        String dial = Application.getApplication().getDeviceModel("dial");//设备类型
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = 0;
        float scaleHeight = 0;
        if (dial.equals("1")) {
            scaleWidth = ((float) zoomBitmapW2 / width);
            scaleHeight = ((float) zoomBitmapH2 / height);
//            scaleWidth = ((float) zoomBitmapW2);
//            scaleHeight = ((float) zoomBitmapH2);
        } else {
            scaleWidth = ((float) zoomBitmapW / width);
            scaleHeight = ((float) zoomBitmapH / height);
//            scaleWidth = ((float) zoomBitmapW);
//            scaleHeight = ((float) zoomBitmapH);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                matrix, true);
        LogUtils.i("ts", "bg指定大小大小" +
                (newBmp.getByteCount() / 1024) +
                "KB宽度为" + newBmp.getWidth() +
                "高度为" + newBmp.getHeight());
        return newBmp;
     * bitmap fg 缩放
     * 指定大小
     * @param bitmap 对象
     * @return newBmp 新 Bitmap对象
    public static Bitmap zoomBitmap2(Bitmap bitmap) {
        String dial = Application.getApplication().getDeviceModel("dial");//设备类型
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = 0;
        float scaleHeight = 0;
        if (dial.equals("1")) {
            scaleWidth = ((float) zoomBitmapFW2 / width);
            scaleHeight = ((float) zoomBitmapFH2 / height);
//            scaleWidth = ((float) zoomBitmapFW2);
//            scaleHeight = ((float) zoomBitmapFH2);
        } else {
            scaleWidth = ((float) zoomBitmapFW / width);
            scaleHeight = ((float) zoomBitmapFH / height);
//            scaleWidth = ((float) zoomBitmapFW);
//            scaleHeight = ((float) zoomBitmapFH);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                matrix, true);
        LogUtils.i("ts", "fg指定大小大小" +
                (newBmp.getByteCount() / 1024) +
                "KB宽度为" + newBmp.getWidth() +
                "高度为" + newBmp.getHeight());
        return newBmp;
     * @param background 背景图
     * @param foreground 前景图
     *                   RGB_565 缩放
     * @return
    public static Bitmap combineBitmap(Bitmap background, Bitmap foreground) {
        if (background == null || background.isRecycled()
                || foreground == null || foreground.isRecycled()) {
            LogUtils.e("gy", "background=" + background + ";foreground=" + foreground);
            return null;
        Bitmap bitmap = background.copy(Bitmap.Config.ARGB_8888, true);//ARGB_8888, RGB_565
        Canvas canvas = new Canvas(bitmap);
        Rect baseRect = new Rect(0, 0, background.getWidth(), background.getHeight());
        Rect frontRect = new Rect(0, 0, foreground.getWidth(), foreground.getHeight());
        canvas.drawBitmap(foreground, frontRect, baseRect, null);
        LogUtils.i("ts", "fg RGB_565格式预览图片的大小" +
                (bitmap.getByteCount() / 1024) +
                "KB宽度为" + bitmap.getWidth() +
                "高度为" + bitmap.getHeight());
        return bitmap;
     * @param background 背景图
     *                   RGB_565 缩放
     * @return
    public static Bitmap combineBitmap2(Bitmap background) {
        if (background == null || background.isRecycled()) {
            LogUtils.e("gy", "background=" + background);
            return null;
        Bitmap bitmap = background.copy(Bitmap.Config.ARGB_8888, true);//ARGB_8888, RGB_565
        LogUtils.i("ts", "bg RGB_565格式预览图片的大小" +
                (bitmap.getByteCount() / 1024) +
                "KB宽度为" + bitmap.getWidth() +
                "高度为" + bitmap.getHeight());
        return bitmap;
     * @param background 背景图
     *                   RGB_565 缩放
     * @return
    public static Bitmap combineBitmap3(Bitmap background) {
        if (background == null || background.isRecycled()) {
            LogUtils.e("gy", "background=" + background);
            return null;
        Bitmap bitmap = background.copy(Bitmap.Config.RGB_565, true);//ARGB_8888, RGB_565
        LogUtils.i("ts", "bg RGB_565格式预览图片的大小" +
                (bitmap.getByteCount() / 1024) +
                "KB宽度为" + bitmap.getWidth() +
                "高度为" + bitmap.getHeight());
        return bitmap;
     * 通过保存格式压缩
     * @param bitmap
     * @return
    public static Bitmap getBitmapByFormat(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        LogUtils.i("ts", "PNG保存格式压缩图片的大小" +
                (bit.getByteCount() / 1024) +
                "KB宽度为" + bit.getWidth() +
                "高度为" + bit.getHeight());
        return bit;
     * 将图片文件路径,转化为字节数组
     * @param path 图片路径
     * @return
    public static byte[] getByteFromPath(String path) {
        byte[] data = null;
        byte[] buffer = new byte[1024];
        int len = -1;
        try {
            // 读取图片字节数组
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            InputStream inStream = new FileInputStream(path);
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            data = outStream.toByteArray();
            outStream.close();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        return data;
     * 将图片,文件路径,转化为字节数组
     * @param path 图片,文件路径
     * @return
    public static byte[] getByteFromPath2(String path) {
        byte[] data = null;
        byte[] buffer = new byte[1024];
        int len = -1;
        try {
            // 读取图片字节数组
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            //InputStream inStream = new FileInputStream(outputFile + path);
            InputStream inStream = new FileInputStream(path);
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            data = outStream.toByteArray();
            outStream.close();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        return data;
    public static byte[] getByteFromUri(Context context, Uri uri) {
        return getByteFromBit(getBitmapFromUri(context, uri));
     * 将drawable转化为Bitmap
     * @return
    public static Bitmap getBitmapFromDrawable(Context context, int image_id) {
        try {
//          return BitmapFactory.decodeResource(context.getResources(), image_id);
            Bitmap bit = BitmapFactory.decodeResource(context.getResources(), image_id);
            return zoomBitmap(bit);
        } catch (Exception e) {
            return null;
    public static Bitmap getExternalFilesDirUriBitmap(Context context, Uri insertUri) {
        ParcelFileDescriptor pfd = null;
        Bitmap bitmap = null;
        if (insertUri != null) {
            try {
                pfd = context.getContentResolver().openFileDescriptor(insertUri, "r");
                if (pfd != null) {
                    bitmap = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
                    // show the bitmap, or do something else.
            } catch (IOException e) {
                LogUtils.e("wl", "getExternalFilesDirUriBitmap IOException: " + e.getMessage());
            } finally {
                try {
                    if (pfd != null) {
                        pfd.close();
                } catch (IOException e) {
                    LogUtils.e("wl", "getExternalFilesDirUriBitmap IOException in close: " + e.getMessage());
        return bitmap;
    public static Bitmap getBitmapFromUri(Context context, Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
            LogUtils.i("ts", "bg原图片大小:" + bitmap.getByteCount() / 1024 + "KB");
        } catch (FileNotFoundException | NullPointerException e) {
            e.printStackTrace();
            //Toast.makeText(context, context.getString(R.string.util_imagedispose_pick_error), Toast.LENGTH_LONG).show();
            return null;
        return bitmap;
     * 字节数组换为Bitmap
     * @param bytes
     * @param opts
     * @return Bitmap
    public static Bitmap getBitmapFromBytes(byte[] bytes, BitmapFactory.Options opts) {
        if (bytes != null)
            if (opts != null) {
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
            } else {
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return null;
     * 字节数组,保存文件 (目前用于网络头像)
     * outputFile
    public static File getFileFromBytes(byte[] b) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(outputFile);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            file = new File(file_folder, outputPicName);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        return file;
     * 字节数组,保存文件
     * outputFile
    public static File getFileFromBytes2(byte[] b, String folder, String fileName) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(folder);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            file = new File(folder, fileName);
            if (file.exists()) {
                file.delete();
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        return file;
     * 字节数组,保存png文件
     * outputFile
    public static File getFileFromBytesPng(byte[] b, int index) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(outputFile);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            if (index == 0) {
                file = new File(outputFile + outputPicName0);
            } else {
                file = new File(outputFile + outputPicName1);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        return file;
     * 字节数组,保存png文件
     * outputFile
    public static File getFileFromBytesPng2(byte[] b, int index, String parent) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(parent);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            if (index == 0) {
                file = new File(file_folder, outputPicName0);
            } else {
                file = new File(file_folder, outputPicName1);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        return file;
     * 字节数组,保存png文件
     * outputFile
    public static File getFileFromBytesPng3(byte[] b, String parent, String fileName) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(parent);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            file = new File(file_folder, fileName);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        return file;
     * 字节数组,保存文件, 再转字节数组
     * outputFile
    public static byte[] getBytesForFileFromBytes(byte[] b) {
        BufferedOutputStream stream = null;
        File file_folder = null;
        File file = null;
        try {
            file_folder = new File(outputFile);
            if (!file_folder.exists()) {
                file_folder.mkdirs();
            file = new File(outputFile + outputPicName2);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
        byte[] data = null;
        byte[] buffer = new byte[1024];
        int len = -1;
        try {
            // 读取图片字节数组
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            InputStream inStream = new FileInputStream(file);
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            data = outStream.toByteArray();
            outStream.close();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        return data;
    public static byte[] readFileFromAssets(Context context, int stylePosition, int imgPosition) {
        String dial = Application.getApplication().getDeviceModel("dial");//设备类型
        String path = "";
        if ("1".equals(dial)) {
            path = "wall/up6/" + stylePosition + "/" + imgPosition + ".data";
        } else {
            path = "wall/up9/" + stylePosition + "/" + imgPosition + ".data";
        byte[] buffer = null;
        try {
            InputStream inputStream = null;
            inputStream = context.getAssets().open(path);
            int length = inputStream.available();
            buffer = new byte[length];
            inputStream.read(buffer);
        } catch (Exception exception) {
            exception.printStackTrace();
            LogUtils.e("gy", "wallPath: " + exception.getMessage());
            return null;
        return buffer;
    public static byte[] readFileFromAssets2(Context context, String imgId) {
        String dial = Application.getApplication().getDeviceModel("dial");//设备类型
        String path = "test/" + imgId + ".png";
        byte[] buffer = null;
        try {
            InputStream inputStream = null;
            inputStream = context.getAssets().open(path);
            int length = inputStream.available();
            buffer = new byte[length];
            inputStream.read(buffer);
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        return buffer;
     * 获取保存的SD图片preview.png转bitmap显示
     * @param path
     * @return
    public static Bitmap fileAsBitmap(String path) {
        Bitmap bitmap = null;
        int w = 100;
        int h = 100;
        String filePath = outputFile + path;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        try {
            // 设置为ture只获取图片大小
//            opts.inJustDecodeBounds = true;
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            // 返回为空
            BitmapFactory.decodeFile(filePath, opts);
            //int width = opts.outWidth;
            //int height = opts.outHeight;
//            float scaleWidth = 0.f, scaleHeight = 0.f;
//            if (width > w || height > h) {
//                // 缩放
//                scaleWidth = ((float) width) / w;
//                scaleHeight = ((float) height) / h;
//            }
            //opts.inJustDecodeBounds = false;
//            //float scale = Math.max(scaleWidth, scaleHeight);
            //float scale = Math.max(width, height);
            //opts.inSampleSize = (int) scale;
            WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(filePath, opts));
            bitmap = Bitmap.createScaledBitmap(weak.get(), 150, 150, true);
        } catch (Exception e) {
            e.printStackTrace();
        return bitmap;
     * 获取保存的SD图片preview.png转bitmap显示
     * @param path
     * @return
    public static Bitmap fileAsBitmap2(String path) {
        Bitmap bitmap = null;
        int w = 100;
        int h = 100;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        try {
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            // 返回为空
            BitmapFactory.decodeFile(path, opts);
            WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
            bitmap = Bitmap.createScaledBitmap(weak.get(), 150, 150, true);
        } catch (Exception e) {
            e.printStackTrace();