-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.java
More file actions
229 lines (202 loc) · 8.65 KB
/
ImageLoader.java
File metadata and controls
229 lines (202 loc) · 8.65 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.coderzf1.colordropper;
import android.app.Activity;
import android.app.Application;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class ImageLoader {
public interface ImageLoaderListener{
void onImageSaved(String filename, Bitmap bmp);
void onImageLoaded(Bitmap bmp);
void onError(String error);
}
Bitmap loadedImage = null;
int error = 0;
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
listener.onImageLoaded(loadedImage);
} else if (msg.what == 1){
listener.onError("Error downloading Image");
}
}
};
Handler handler2 = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg){
if (msg.what==0){
Bundle bundle = msg.getData();
String filename = bundle.getString("filename");
listener.onImageSaved(filename ,loadedImage);
}else if(msg.what==1){
listener.onError("Error downloading Image");
}
}
};
private ImageLoaderListener listener;
public ImageLoader(){
listener = null;
}
public void setImageLoaderListener(ImageLoaderListener listener){
this.listener = listener;
}
private void saveImageToFile(Context context, Bitmap bmp, String filename){
try {
saveImage(context, bmp, getApplicationName(context), filename);
} catch (IOException e) {
e.printStackTrace();
}
}
private String getApplicationName(Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
int stringId = applicationInfo.labelRes;
return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
}
//The following code was found online+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Slightly modified by me
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private Uri saveImage(Context context, Bitmap bitmap, @NonNull String folderName, @NonNull String fileName) throws IOException {
OutputStream fos = null;
File imageFile = null;
Uri imageUri = null;
Bitmap.CompressFormat format = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(fileName);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE,mimeType) ;
contentValues.put(
MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + folderName);
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
if (imageUri == null)
throw new IOException("Failed to create new MediaStore record.");
fos = resolver.openOutputStream(imageUri);
} else {
File imagesDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString() + File.separator + folderName);
String compressionName = mimeType.toLowerCase().replace("image/", "").toUpperCase();
Log.println(Log.DEBUG,"compressFormat: ", compressionName);
//try to determine compression format from original file
try {
format = Bitmap.CompressFormat.valueOf(compressionName);
}catch (Exception e){
format = Bitmap.CompressFormat.PNG;
extension = "png";
}
if (!imagesDir.exists())
imagesDir.mkdir();
imageFile = new File(imagesDir, fileName + "." + extension);
fos = new FileOutputStream(imageFile);
}
if (!bitmap.compress(format, 100, fos))
throw new IOException("Failed to save bitmap.");
fos.flush();
} finally {
if (fos != null)
fos.close();
}
if (imageFile != null) {//pre Q
MediaScannerConnection.scanFile(context, new String[]{imageFile.toString()}, null, null);
imageUri = Uri.fromFile(imageFile);
}
return imageUri;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void downloadAndSaveImageFromUrl(Context context, String url){
Uri uri = Uri.parse(url);
final String filename = uri.getLastPathSegment();
new Thread(() -> {
Log.println(Log.DEBUG, "Start","Start Download from '" + url + "'");
int counter = 0;
Bitmap bmp = null;
Message msg = new Message();
try {
Log.println(Log.DEBUG,"Download","Attempting to download image");
bmp = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
msg.what = 0;
Bundle bundle = new Bundle();
bundle.putString("filename",filename);
msg.setData(bundle);
} catch (IOException e) {
e.printStackTrace();
//bmp = BitmapFactory.decodeResource(activity.getResources(),R.drawable.imgnotfound);
msg.what = 1;
Log.println(Log.ERROR,"ERROR:","Image not found. Setting to Default image.");
}
while (counter < 10){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter +=1;
if (bmp != null || error ==1){
break;
}
}
Log.println(Log.DEBUG,"DEBUG","Calling Listener");
loadedImage = bmp;
if (loadedImage != null){
saveImageToFile(context,loadedImage,filename);
}
handler2.sendMessage(msg);
}).start();
}
public void loadImageFromUrl(String url){
new Thread(() -> {
Log.println(Log.DEBUG, "Start","Start Download from '" + url + "'");
int counter = 0;
Bitmap bmp = null;
try {
Log.println(Log.DEBUG,"Download","Attempting to download image");
bmp = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
error = 0;
} catch (IOException e) {
e.printStackTrace();
//bmp = BitmapFactory.decodeResource(activity.getResources(),R.drawable.imgnotfound);
error = 1;
Log.println(Log.ERROR,"ERROR:","Image not found. Setting to Default image.");
}
while (counter < 10){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter +=1;
if (bmp != null || error ==1){
break;
}
}
Log.println(Log.DEBUG,"DEBUG","Calling Listener");
loadedImage = bmp;
handler.sendEmptyMessage(error);
}).start();
}
}