Skip to content

Commit 4454b2e

Browse files
committed
Fix wrong thread issue for compress
1 parent 55ba640 commit 4454b2e

File tree

2 files changed

+139
-91
lines changed

2 files changed

+139
-91
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.github.mikephil.charting.renderer.DataRenderer;
4545
import com.github.mikephil.charting.renderer.LegendRenderer;
4646
import com.github.mikephil.charting.utils.MPPointF;
47+
import com.github.mikephil.charting.utils.SaveUtils;
4748
import com.github.mikephil.charting.utils.Utils;
4849
import com.github.mikephil.charting.utils.ViewPortHandler;
4950

@@ -1413,27 +1414,7 @@ public Bitmap getChartBitmap() {
14131414
* @return returns true on success, false on error
14141415
*/
14151416
public boolean saveToPath(String title, String pathOnSD) {
1416-
1417-
1418-
Bitmap b = getChartBitmap();
1419-
1420-
OutputStream stream;
1421-
try {
1422-
stream = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + pathOnSD + "/" + title + ".png");
1423-
1424-
/*
1425-
* Write bitmap to file using JPEG or PNG and 40% quality hint for
1426-
* JPEG.
1427-
*/
1428-
b.compress(CompressFormat.PNG, 40, stream);
1429-
1430-
stream.close();
1431-
} catch (Exception e) {
1432-
e.printStackTrace();
1433-
return false;
1434-
}
1435-
1436-
return true;
1417+
return SaveUtils.INSTANCE.saveToPath(title, pathOnSD, getChartBitmap());
14371418
}
14381419

14391420
/**
@@ -1449,76 +1430,7 @@ public boolean saveToPath(String title, String pathOnSD) {
14491430
* @return returns true if saving was successful, false if not
14501431
*/
14511432
public boolean saveToGallery(String fileName, String subFolderPath, String fileDescription, Bitmap.CompressFormat format, int quality) {
1452-
// restrain quality
1453-
if (quality < 0 || quality > 100) {
1454-
quality = 50;
1455-
}
1456-
1457-
long currentTime = System.currentTimeMillis();
1458-
1459-
File extBaseDir = Environment.getExternalStorageDirectory();
1460-
File file = new File(extBaseDir.getAbsolutePath() + "/DCIM/" + subFolderPath);
1461-
if (!file.exists()) {
1462-
if (!file.mkdirs()) {
1463-
return false;
1464-
}
1465-
}
1466-
1467-
String mimeType;
1468-
switch (format) {
1469-
case PNG:
1470-
mimeType = "image/png";
1471-
if (!fileName.endsWith(".png")) {
1472-
fileName += ".png";
1473-
}
1474-
break;
1475-
case WEBP:
1476-
mimeType = "image/webp";
1477-
if (!fileName.endsWith(".webp")) {
1478-
fileName += ".webp";
1479-
}
1480-
break;
1481-
case JPEG:
1482-
default:
1483-
mimeType = "image/jpeg";
1484-
if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) {
1485-
fileName += ".jpg";
1486-
}
1487-
break;
1488-
}
1489-
1490-
String filePath = file.getAbsolutePath() + "/" + fileName;
1491-
FileOutputStream out;
1492-
try {
1493-
out = new FileOutputStream(filePath);
1494-
1495-
Bitmap b = getChartBitmap();
1496-
b.compress(format, quality, out);
1497-
1498-
out.flush();
1499-
out.close();
1500-
1501-
} catch (IOException e) {
1502-
e.printStackTrace();
1503-
1504-
return false;
1505-
}
1506-
1507-
long size = new File(filePath).length();
1508-
1509-
ContentValues values = new ContentValues(8);
1510-
1511-
// store the details
1512-
values.put(Images.Media.TITLE, fileName);
1513-
values.put(Images.Media.DISPLAY_NAME, fileName);
1514-
values.put(Images.Media.DATE_ADDED, currentTime);
1515-
values.put(Images.Media.MIME_TYPE, mimeType);
1516-
values.put(Images.Media.DESCRIPTION, fileDescription);
1517-
values.put(Images.Media.ORIENTATION, 0);
1518-
values.put(Images.Media.DATA, filePath);
1519-
values.put(Images.Media.SIZE, size);
1520-
1521-
return getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values) != null;
1433+
return SaveUtils.INSTANCE.saveToGallery(fileName, subFolderPath, fileDescription, format, quality, getChartBitmap(), getContext());
15221434
}
15231435

15241436
/**
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.github.mikephil.charting.utils
2+
3+
import android.content.ContentValues
4+
import android.content.Context
5+
import android.graphics.Bitmap
6+
import android.graphics.Bitmap.CompressFormat
7+
import android.os.Environment
8+
import android.provider.MediaStore
9+
import java.io.File
10+
import java.io.FileOutputStream
11+
import java.io.IOException
12+
import java.io.OutputStream
13+
14+
object SaveUtils {
15+
16+
/**
17+
* Saves the current chart state with the given name to the given path on
18+
* the sdcard leaving the path empty "" will put the saved file directly on
19+
* the SD card chart is saved as a PNG image, example:
20+
* saveToPath("myfilename", "foldername1/foldername2");
21+
*
22+
* @param pathOnSD e.g. "folder1/folder2/folder3"
23+
* @return returns true on success, false on error
24+
*/
25+
fun saveToPath(title: String?, pathOnSD: String?, chartBitmap: Bitmap): Boolean {
26+
val stream: OutputStream?
27+
try {
28+
stream = FileOutputStream(Environment.getExternalStorageDirectory().path + pathOnSD + "/" + title + ".png")
29+
30+
/*
31+
* Write bitmap to file using JPEG or PNG and 40% quality hint for JPEG.
32+
*/
33+
chartBitmap.compress(CompressFormat.PNG, 40, stream)
34+
35+
stream.close()
36+
return true
37+
} catch (e: Exception) {
38+
e.printStackTrace()
39+
return false
40+
}
41+
}
42+
43+
/**
44+
* Saves the current state of the chart to the gallery as an image type. The
45+
* compression must be set for JPEG only. 0 == maximum compression, 100 = low
46+
* compression (high quality). NOTE: Needs permission WRITE_EXTERNAL_STORAGE
47+
*
48+
* @param fileName e.g. "my_image"
49+
* @param subFolderPath e.g. "ChartPics"
50+
* @param fileDescription e.g. "Chart details"
51+
* @param format e.g. Bitmap.CompressFormat.PNG
52+
* @param quality e.g. 50, min = 0, max = 100
53+
* @return returns true if saving was successful, false if not
54+
*/
55+
fun saveToGallery(fileName: String, subFolderPath: String?, fileDescription: String?, format: CompressFormat, quality: Int, chartBitmap: Bitmap, context: Context): Boolean {
56+
// restrain quality
57+
var fileName = fileName
58+
var quality = quality
59+
if (quality < 0 || quality > 100) {
60+
quality = 50
61+
}
62+
63+
val currentTime = System.currentTimeMillis()
64+
65+
val extBaseDir = Environment.getExternalStorageDirectory()
66+
val file = File(extBaseDir.absolutePath + "/DCIM/" + subFolderPath)
67+
if (!file.exists()) {
68+
if (!file.mkdirs()) {
69+
return false
70+
}
71+
}
72+
73+
val mimeType: String?
74+
when (format) {
75+
CompressFormat.PNG -> {
76+
mimeType = "image/png"
77+
if (!fileName.endsWith(".png")) {
78+
fileName += ".png"
79+
}
80+
}
81+
82+
CompressFormat.WEBP -> {
83+
mimeType = "image/webp"
84+
if (!fileName.endsWith(".webp")) {
85+
fileName += ".webp"
86+
}
87+
}
88+
89+
CompressFormat.JPEG -> {
90+
mimeType = "image/jpeg"
91+
if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) {
92+
fileName += ".jpg"
93+
}
94+
}
95+
96+
else -> {
97+
mimeType = "image/jpeg"
98+
if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))) {
99+
fileName += ".jpg"
100+
}
101+
}
102+
}
103+
104+
val filePath = file.absolutePath + "/" + fileName
105+
val out: FileOutputStream?
106+
try {
107+
out = FileOutputStream(filePath)
108+
109+
chartBitmap.compress(format, quality, out)
110+
111+
out.flush()
112+
out.close()
113+
} catch (e: IOException) {
114+
e.printStackTrace()
115+
116+
return false
117+
}
118+
119+
val size = File(filePath).length()
120+
121+
val values = ContentValues(8)
122+
123+
// store the details
124+
values.put(MediaStore.Images.Media.TITLE, fileName)
125+
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
126+
values.put(MediaStore.Images.Media.DATE_ADDED, currentTime)
127+
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType)
128+
values.put(MediaStore.Images.Media.DESCRIPTION, fileDescription)
129+
values.put(MediaStore.Images.Media.ORIENTATION, 0)
130+
values.put(MediaStore.Images.Media.DATA, filePath)
131+
values.put(MediaStore.Images.Media.SIZE, size)
132+
133+
return context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) != null
134+
}
135+
136+
}

0 commit comments

Comments
 (0)