美文网首页
将某个布局存为图片(解决实时通知相册时出现两张图片的bug)

将某个布局存为图片(解决实时通知相册时出现两张图片的bug)

作者: jdallen | 来源:发表于2019-07-26 09:58 被阅读0次
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 将某个布局存为图片
 *
 * @author jdallen
 * @since 2019/4/30
 */

public class ViewToPictureUtil {

    public static void viewSaveToImage(Activity activity, View view, int type, SavePictureToLocalListener savePictureToLocalListener) {
        // 把一个View转换成图片
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        //view.setDrawingCacheBackgroundColor(Color.BLACK);
        //修复布局过大,导致生成的Bitmap为空的bug
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(WindowUtil.getWindowWidth(activity), View.MeasureSpec.EXACTLY);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((int) (WindowUtil.getWindowHeight(activity) * 0.82), View.MeasureSpec.EXACTLY);
        view.measure(widthMeasureSpec, heightMeasureSpec);
        //view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        //根据View生成Bitmap
        Bitmap bitmap = view.getDrawingCache();
        // 判断手机设备是否有SD卡
        boolean isHasSDCard = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if (isHasSDCard) {
            //系统相册目录
            try {
                File file = createImageFile();
                FileOutputStream fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
                // 保存过后释放资源
                view.destroyDrawingCache();
                // 其次把文件插入到系统图库
                String fileName = file.getName();
                LogHelper.e("ATU图片路径:", fileName);
                //保存
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
                values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                //最后通知图库更新
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);//扫描单个文件
                intent.setData(Uri.parse("file://" + file.getAbsolutePath()));
                activity.sendBroadcast(intent);
                savePictureToLocalListener.saveSuccess("已将收款码保存到相册");
            } catch (Exception e) {
                e.printStackTrace();
                savePictureToLocalListener.saveFailure("生成图片抛出异常" + e.toString());
            }
        } else {

            savePictureToLocalListener.saveFailure("您的手机没有SD卡,无法保存收款码");
        }
    }

    private static File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + ".jpg";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        if (!storageDir.exists()) {
            if (!storageDir.mkdir()) {
                LogHelper.e("TAG", "Throwing Errors....");
                throw new IOException();
            }
        }
        LogHelper.e("ATU图片路径:", imageFileName);
        return new File(storageDir, imageFileName);
    }


    public interface SavePictureToLocalListener {
        void saveSuccess(String msg);

        void saveFailure(String errMsg);
    }

}
import android.content.Context;
import android.view.WindowManager;

/**
 * 屏幕宽高管理类
 *
 * @author jdallen
 * @since 2019/3/21
 */

public class WindowUtil {

    public static int getWindowHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    public static int getWindowWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }
}

相关文章

网友评论

      本文标题:将某个布局存为图片(解决实时通知相册时出现两张图片的bug)

      本文链接:https://www.haomeiwen.com/subject/omkkrctx.html