In the model object there is a list of objects that each one can contain internal and external images.
I read that I can not process a url request in the main thread, so I thought of using AsyncTask
.
But, I also need to know when one image was processed... so I thought of using the execute().get()
, but I also think that this is not working. I also thought that I would be able to download and storing all the images sequential with the source code that is bellow...
But somehow, I the main activity receives the answer before all the downloads are completed.
And to make things stranger, yes, I am able to download/save the images, but somehow, they all have the same size 48bits and when I try to open the image everything is "black".... I think that somehow, there are shared variables with this threading....
I am sure that my code is full of flaws and some part of the code is repeated ( I could do some code refactoring)... but... for now I am only looking to make it work.
public void ProcessImages(MyModel model) {
if (model == null || model.History == null || model.History .size() == 0) {
return;
}
model.History .forEach((h) -> {
if (h.InternalImages != null && h.InternalImages.size() > 0) {
h.InternalImages.forEach((i) -> {
ExecuteSaveToInternalStorage(i);
return;
});
}
if (h.ExternalImages != null && h.ExternalImages.size() > 0) {
h.ExternalImages.forEach((i) -> {
ExecuteSaveToInternalStorage(i);
return;
});
}
});
}
.
public void ExecuteSaveToInternalStorage(LKImage lkImage){
try {
(new AsyncTask<Void,Void,Void>() {
@Override
protected Void doInBackground(Void... voids) {
SaveToInternalStorage(lkImage);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
//multiTaskHandler.taskComplete();
}
}).execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
.
public boolean SaveToInternalStorage(LKImage lkImage) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + File.separator + ImageDirectory;
File outputDir = new File(path);
if (!outputDir.exists()) {
outputDir.mkdir();
}
boolean res = true;
File imageThumb = new File(path + File.separator + "thumb" + "_" + lkImage.Id + "_" + lkImage.Name);
if (!imageThumb.exists()) {
try {
InputStream inputStream = new java.net.URL(GetAPIServerUrl() + "/Images/GetImage/" + lkImage.Id + "?s=" + lkImage.ImageSource + "&t=True&type=.jpg&tId=" + lkImage.TenantId).openStream();
Bitmap bitmapImage = BitmapFactory.decodeStream(inputStream);
FileOutputStream out = new FileOutputStream(imageThumb);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
res = false;
}
}
File imageBig = new File(path + File.separator + lkImage.Id + "_" + lkImage.Name);
if (!imageBig.exists()) {
try {
InputStream inputStream = new java.net.URL(GetAPIServerUrl() + "/Images/GetImage/" + lkImage.Id + "?s=" + lkImage.ImageSource + "&t=False&type=.jpg&tId=" + lkImage.TenantId).openStream();
Bitmap bitmapImage = BitmapFactory.decodeStream(inputStream);
FileOutputStream out = new FileOutputStream(imageBig);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
res = false;
}
}
return res;
}