mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-23 12:33:53 +00:00
Fixes ui freeze while image is downloaded
This commit is contained in:
1 parent
5b458c64b1
commit
af9e6003ed
2 files changed
+69
-14
No files matched your search
+23
-14
@@ -11,10 +11,7 @@ import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.*;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
@@ -36,6 +33,7 @@ import com.sap.sailing.android.tracking.app.BuildConfig;
|
||||
import com.sap.sailing.android.tracking.app.R;
|
||||
import com.sap.sailing.android.tracking.app.ui.fragments.RegattaFragment;
|
||||
import com.sap.sailing.android.tracking.app.utils.AppPreferences;
|
||||
import com.sap.sailing.android.tracking.app.utils.BitmapHelper;
|
||||
import com.sap.sailing.android.tracking.app.utils.CheckinManager;
|
||||
import com.sap.sailing.android.tracking.app.utils.DatabaseHelper;
|
||||
import com.sap.sailing.android.tracking.app.valueobjects.CheckinData;
|
||||
@@ -327,7 +325,6 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
if (pictureFile == null || !pictureFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
||||
Bitmap image = BitmapFactory.decodeFile(pictureFile.getAbsolutePath(), options);
|
||||
@@ -478,7 +475,7 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
}
|
||||
}
|
||||
|
||||
private class DownloadLeaderboardImageTask extends AsyncTask<String, Void, Bitmap> {
|
||||
private class DownloadLeaderboardImageTask extends AsyncTask<String, Void, File> {
|
||||
ImageView bmImage;
|
||||
String downloadUrl;
|
||||
ProgressDialog dialog;
|
||||
@@ -491,27 +488,39 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
dialog = new ProgressDialog(bmImage.getContext(), R.style.Base_Theme_AppCompat_Dialog);
|
||||
dialog.setCancelable(false);
|
||||
dialog.setMessage(getString(R.string.download_team_image_message));
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
protected Bitmap doInBackground(String... urls) {
|
||||
protected File doInBackground(String... urls) {
|
||||
downloadUrl = urls[0];
|
||||
Bitmap mIcon11 = null;
|
||||
File imageFile = null;
|
||||
|
||||
try {
|
||||
InputStream in = new java.net.URL(downloadUrl).openStream();
|
||||
mIcon11 = BitmapFactory.decodeStream(in);
|
||||
imageFile = getImageFile(getLeaderboardImageFileName(leaderboard.name));
|
||||
if (!imageFile.exists()){
|
||||
imageFile.createNewFile();
|
||||
}
|
||||
FileOutputStream outputStream = new FileOutputStream(imageFile);
|
||||
int read = 0;
|
||||
byte[] bytes = new byte[1024];
|
||||
|
||||
while ((read = in.read(bytes)) != -1) {
|
||||
outputStream.write(bytes, 0, read);
|
||||
}
|
||||
outputStream.close();
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mIcon11;
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
protected void onPostExecute(Bitmap result) {
|
||||
if (result != null) {
|
||||
bmImage.setImageBitmap(result);
|
||||
storeImageAndSendToServer(result, getLeaderboardImageFileName(leaderboard.name), false);
|
||||
protected void onPostExecute(File result) {
|
||||
if (result != null && result.exists()) {
|
||||
bmImage.setImageBitmap(BitmapHelper.getBitmapFromFile(result, bmImage.getMeasuredWidth(), bmImage.getMeasuredHeight()));
|
||||
userImageUpdated();
|
||||
} else {
|
||||
ExLog.e(RegattaActivity.this, TAG, "Failed to download leaderboard image at url " + downloadUrl);
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.sap.sailing.android.tracking.app.utils;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class BitmapHelper {
|
||||
public static Bitmap getBitmapFromFile(File file, int requiredWidth, int requiredHeight){
|
||||
// First decode with inJustDecodeBounds=true to check dimensions
|
||||
final BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFile(file.getPath(), options);
|
||||
|
||||
// Calculate inSampleSize
|
||||
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
|
||||
|
||||
// Decode bitmap with inSampleSize set
|
||||
options.inJustDecodeBounds = false;
|
||||
return BitmapFactory.decodeFile(file.getPath(), options);
|
||||
}
|
||||
|
||||
// Source: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
|
||||
private static int calculateInSampleSize(
|
||||
BitmapFactory.Options options, int reqWidth, int reqHeight) {
|
||||
// Raw height and width of image
|
||||
final int height = options.outHeight;
|
||||
final int width = options.outWidth;
|
||||
int inSampleSize = 1;
|
||||
|
||||
if (height > reqHeight || width > reqWidth) {
|
||||
|
||||
final int halfHeight = height / 2;
|
||||
final int halfWidth = width / 2;
|
||||
|
||||
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
|
||||
// height and width larger than the requested height and width.
|
||||
while ((halfHeight / inSampleSize) > reqHeight
|
||||
&& (halfWidth / inSampleSize) > reqWidth) {
|
||||
inSampleSize *= 2;
|
||||
}
|
||||
}
|
||||
return inSampleSize;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user