Android 与H5交互心得(Base64图片)

1,图片转Base64字符串;

a,Android 转Base64字符串:

1)将本地文件转化为Base64字符串;
public static String fileToBase64(String path) {
    if (TextUtils.isEmpty(path)){
        return null;
    }

    FileInputStream fis = null;
    int byteSize = 0;
    try {
        fis = new FileInputStream(path);
        byteSize = fis.available();
    } catch (IOException e) {
        e.printStackTrace();
    }
    int size = byteSize / MB;
    Bitmap bitmap  = BitmapFactory.decodeStream(fis);
    if (size > 5) {
        // 当图片大于5兆时,进行尺寸压缩(做个压缩处理,转化Base64字符串会快一些)
        bitmap = getScaleBitmap(bitmap);
    }

    String bitmapStr = "";
    if (bitmap != null) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            if (size > 5) {
                // 当图片大于5兆时,进行质量压缩
                int multiple = size / 5;
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100 / multiple, outputStream);
            } else {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            }
            bitmapStr = Base64.getEncoder().encodeToString(outputStream.toByteArray());
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
    return bitmapStr;
}
2)对bitmap做尺寸压缩
public static Bitmap getScaleBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        //将大于1080px的图片转化为1080px的大小,这里可以根据业务做修改
        final int minSize = 1080;
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int scaleWidth;
        int scaleHeight;
        int tempSize = Math.min(bitmapWidth, bitmapHeight);
        float scaleFactor = (float) minSize / (float) tempSize;
        //判断最大的长或宽
        if (tempSize == bitmapWidth) {
            if (tempSize > minSize) {
                scaleWidth = minSize;
                scaleHeight = (int) (bitmapHeight * scaleFactor);
            } else {
                return bitmap;
            }
        } else {
            if (tempSize > minSize) {
                scaleHeight = minSize;
                scaleWidth = (int) (bitmapWidth * scaleFactor);
            } else {
                return bitmap;
            }
        }
        Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true);
        
        return scaleBitmap;
    }

压缩图片是为了提升Base64字符串的转化速度,但是如果对图片的像素有要求,则不进行压缩处理;

转化后的Base64字符传,H5是无法直接使用的;
H5加载Base64图片的格式为:
后面的(...)就是我们传给H5的Base64字符串;
所以要不就是我们把前面那一截加上传给H5,要不就是H5自己把前面那一截拼接上去;
最好是由H5那边处理,可以定义图片的类型;

b,将H5传过来的Base64字符串转成图片保存到本地:

1)将H5传过来的字符串转化为bitmap,并保存到本地;

H5传给Android的Base64字符串是(data:image/png;base64,...)这种格式的,如果直接转化为图片保存到本地,则无法正常显示;
在此,我们需要对传过来的字符传进行处理,将前面那一截去掉,只留(...)省略号那一截;

public static Bitmap convertStr2Bitmaph5(String str) {
        Bitmap bitmap = null;
        if (!TextUtils.isEmpty(str)) {
            try {
                //将H5传过来的Base64字符传进行处理
                int indexOf = str.lastIndexOf(",");
                String substring = str.substring(indexOf + 1);
                
                // getDecoder: 可以解码+和/的情况 (2d); getUrlDecoder: 可以解码-和_的情况 (5f)
                byte[] bytes = Base64.getDecoder().decode(substring);
                bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
2) 将bitmap保存到本地
public static void saveBitmapToFile(Bitmap bm, Bitmap.CompressFormat format, String path, String fileName) {
        if (null == bm) {
            return;
        }
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            File dirFile = new File(path);
            if (!dirFile.exists()) {
                FileUtil.mkdirs(dirFile);
            }
            File myCaptureFile = new File(path + fileName);
            if (myCaptureFile.exists()) {
                FileUtil.deleteAll(myCaptureFile);
            }
            fos = new FileInputStream(myCaptureFile);
            bos = new BufferedOutputStream(fos);
            bm.compress(format, 100, bos);
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bm != null) {
                bm.recycle();
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

最后再刷新一下本地相册,即可看到保存的图片;

你可能感兴趣的:(android,html5,经验)