1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
public static void savingBitmapIntoFile(final Bitmap pic1, final Bitmap pic2, final Activity context, final BitmapAndFileCallBack callBack) { if (context == null || context.isFinishing()) { return; } Thread thread = new Thread(new Runnable() { @Override public void run() { String fileReturnPath = ""; int w = pic1.getWidth(); Bitmap bottom = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_picture_combine_bottom); Bitmap top_banner = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_picture_combine_top);
Bitmap bitmap_bottom = anyRatioCompressing(bottom, (float) w / bottom.getWidth(), (float) w / bottom.getWidth()); Bitmap bitmap_top = anyRatioCompressing(top_banner, (float) w / bottom.getWidth(), (float) w / bottom.getWidth()); final Bitmap only_bitmap = combineBitmapsIntoOnlyOne(bitmap_top, pic1, pic2, bitmap_bottom, context);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-ms", Locale.getDefault()); String data = sdf.format(new Date());
String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath(); String fileTitle = "Screenshot_" + data + "_com.bertadata.qxb.biz_info.jpg"; String filePath = storagePath + "/DCIM/"; final String fileAbsolutePath = filePath + fileTitle; File file = new File(fileAbsolutePath);
if (only_bitmap != null) { try { FileOutputStream fos = new FileOutputStream(file); only_bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
String other_fileTitle = "Screenshot_" + data + "_com.bertadata.qxb.jpg"; String other_fileAbsolutePath = filePath + other_fileTitle; File other_file = new File(other_fileAbsolutePath); FileOutputStream other_fos = new FileOutputStream(other_file);
long file_size = file.length() / 1024; if (file_size < 0 || !(file.exists())) { throw new NullPointerException(); } else if (file_size > 0 && file_size <= 256) { deleteFile(other_file); fileReturnPath = fileAbsolutePath; context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileAbsolutePath))); } else if (file_size > 256 && file_size <= 768) { anyRatioCompressing(only_bitmap, (float) 3 / 4, (float) 3 / 4).compress(Bitmap.CompressFormat.JPEG, 40, other_fos); deleteFile(file); fileReturnPath = other_fileAbsolutePath; context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + other_fileAbsolutePath))); } else if (file_size > 768 && file_size <= 1280) { anyRatioCompressing(only_bitmap, (float) 1 / 2, (float) 1 / 2).compress(Bitmap.CompressFormat.JPEG, 40, other_fos); deleteFile(file); fileReturnPath = other_fileAbsolutePath; context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + other_fileAbsolutePath))); } else if (file_size > 1280 && file_size <= 2048) { anyRatioCompressing(only_bitmap, (float) 1 / 3, (float) 1 / 3).compress(Bitmap.CompressFormat.JPEG, 40, other_fos); deleteFile(file); fileReturnPath = other_fileAbsolutePath; context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + other_fileAbsolutePath))); } else if (file_size > 2048) { anyRatioCompressing(only_bitmap, (float) 1 / 2, (float) 1 / 2).compress(Bitmap.CompressFormat.JPEG, 40, other_fos); deleteFile(file); fileReturnPath = other_fileAbsolutePath; context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + other_fileAbsolutePath))); }
fos.flush(); other_fos.flush(); other_fos.close(); fos.close(); callBack.onSuccess(only_bitmap, fileReturnPath); } catch (Exception e) { e.printStackTrace(); } } else callBack.onSuccess(null, ""); } }); thread.start(); }
public static Bitmap anyRatioCompressing(Bitmap bitmap, float width_ratio, float height_ratio) { Matrix matrix = new Matrix(); matrix.postScale(width_ratio, height_ratio); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); }
|