mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-25 05:23:51 +00:00
merged ubilabs--tracking_app--bugfix--image_download_app_freeze
This commit is contained in:
commit
4564efb4cd
4 files changed
+85
-17
No files matched your search
@@ -6,5 +6,6 @@
|
||||
<string name="confirm_data_is_correct">OK</string>
|
||||
<string name="decline_data_is_incorrect">Das bin nicht ich!</string>
|
||||
<string name="confirm_delete_checkin">Möchten Sie den Checkin wirklich löschen?</string>
|
||||
<string name="download_team_image_message">Team Bild wird geladen...</string>
|
||||
|
||||
</resources>
|
||||
@@ -7,4 +7,5 @@
|
||||
<string name="confirm_data_is_correct">OK</string>
|
||||
<string name="decline_data_is_incorrect">That\'s not me!</string>
|
||||
<string name="confirm_delete_checkin">Do you really want to delete this checkin?</string>
|
||||
<string name="download_team_image_message">Downloading team image...</string>
|
||||
</resources>
|
||||
+37
-17
@@ -1,5 +1,7 @@
|
||||
package com.sap.sailing.android.tracking.app.ui.activities;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
@@ -9,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;
|
||||
@@ -34,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;
|
||||
@@ -186,7 +186,6 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
setUpView();
|
||||
RegattaFragment regattaFragment = getRegattaFragment();
|
||||
if (regattaFragment != null) {
|
||||
regattaFragment.setFragmentWatcher(this);
|
||||
@@ -255,7 +254,7 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
try {
|
||||
String teamImageUri = response.getString("imageUri");
|
||||
if (teamImageUri != null) {
|
||||
new DownloadLeaderboardImageTask(imageView).execute(teamImageUri);
|
||||
new DownloadLeaderboardImageTask(imageView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,teamImageUri);
|
||||
}
|
||||
}
|
||||
catch (JSONException e) {
|
||||
@@ -304,7 +303,7 @@ public class RegattaActivity extends AbstractRegattaActivity implements RegattaF
|
||||
}
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(pictureFile);
|
||||
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
|
||||
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
||||
fos.close();
|
||||
if (sendToServer) {
|
||||
sendTeamImageToServer(pictureFile);
|
||||
@@ -326,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);
|
||||
@@ -477,35 +475,57 @@ 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;
|
||||
|
||||
public DownloadLeaderboardImageTask(ImageView bmImage) {
|
||||
this.bmImage = bmImage;
|
||||
}
|
||||
|
||||
protected Bitmap doInBackground(String... urls) {
|
||||
@Override
|
||||
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 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);
|
||||
}
|
||||
dialog.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+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