-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSaveImage.java
More file actions
72 lines (57 loc) · 1.8 KB
/
SaveImage.java
File metadata and controls
72 lines (57 loc) · 1.8 KB
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
package tools.amolood.qrpanda.utility;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import tools.amolood.qrpanda.data.constant.Constants;
/**
* Created by ِAmolod on 7/21/2016.
*/
public class SaveImage extends AsyncTask<Void, Void, String> {
private String name;
private Bitmap bitmap;
private SaveListener saveListener;
public SaveImage(String name, Bitmap bitmap) {
this.name = name;
this.bitmap = bitmap;
}
public void setSaveListener(SaveListener saveListener) {
this.saveListener = saveListener;
}
@Override
protected String doInBackground(Void... voids) {
return saveImageFile(bitmap, name);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(saveListener != null) {
saveListener.onSaved(s);
}
}
public interface SaveListener {
public void onSaved(String savedTo);
}
private String saveImageFile(Bitmap bitmap, String fileName) {
FileOutputStream out = null;
String filePath = getFilename(fileName);
try {
out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
return filePath;
}
private String getFilename(String fileName) {
File file = new File(Environment.getExternalStorageDirectory().getPath(), Constants.SAVE_TO);
if (!file.exists()) {
file.mkdirs();
}
if(fileName.contains("/")) {
fileName = fileName.replace("/", "\\");
}
return (file.getAbsolutePath() + "/" + fileName + ".png");
}
}