!23 fix: 修改拍照数量过多时,数据保存过慢的问题

Merge pull request !23 from 肖岩/founchBranch
This commit is contained in:
王东升 2021-12-03 05:34:16 +00:00 committed by Gitee
commit 1b47add5bf
2 changed files with 50 additions and 55 deletions

View File

@ -80,6 +80,7 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -125,7 +126,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
private int radioPicture = 0;
private int videoIndex = -1;
private int startVideoIndex = -1;
private int webp=1;
@SuppressLint("SimpleDateFormat")
private SimpleDateFormat formatter;
private Handler handler = new Handler(new Handler.Callback() {
@ -327,7 +327,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
result.toFile(file, new FileCallback() {
@Override
public void onFileReady(@Nullable File file) {
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(result, file));
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, 0));
}
});
} else {
@ -348,41 +348,56 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
layoutParamsMap.height = dm.widthPixels / 3;
layoutParamsMap.width = dm.heightPixels / 3;
tvMapView.setLayoutParams(layoutParamsMap);
timerTask = new TimerTask() {
@Override
public void run() {
if (radioPicture == 1) {
camera.takePicture();
} else {
Message message = new Message();
message.what = 0x101;
handler.sendMessage(message);
}
}
};
}
class Jpg2WebpRunnable implements Runnable {
private PictureResult pictureResult;
// private PictureResult pictureResult;
private File file;
private int count; // 该转换执行次数如果连续3次执行失败则不再转换
public Jpg2WebpRunnable(PictureResult pictureResult, File file) {
this.pictureResult = pictureResult;
public Jpg2WebpRunnable(/*PictureResult pictureResult, */File file, int count) {
// this.pictureResult = pictureResult;
this.file = file;
this.count = count;
}
@Override
public void run() {
if (file.exists() && file != null) {
initWeb(file);
runOnUiThread(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
if (PicturesActivity.this != null) {
if (file.exists()) {
initMarkerPaper();
initMarker();
videoIndex = Integer.parseInt(file.getName().replace(".webp", ""));
finalVideoPath = Objects.requireNonNull(file.getParentFile()).getAbsolutePath() + "/" + (videoIndex + 1) + ".webp";
tvTitle.setText("保存成功:" + (videoIndex + 1));
}
if (radioPicture == 1 && handler != null) {
Message message = new Message();
message.what = 0x103;
handler.sendMessage(message);
initWeb(file, count);
if (count<=0) { // 不是重新转换webp流程
runOnUiThread(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
if (PicturesActivity.this != null) {
if (file.exists()) {
initMarker();
videoIndex = Integer.parseInt(file.getName().replace(".webp", ""));
finalVideoPath = Objects.requireNonNull(file.getParentFile()).getAbsolutePath() + "/" + (videoIndex + 1) + ".webp";
tvTitle.setText("保存成功:" + (videoIndex + 1));
}
if (radioPicture == 1 && handler != null) {
Message message = new Message();
message.what = 0x103;
handler.sendMessage(message);
}
}
}
}
});
});
}
} else {
isBack = false;
if (isOration) {
@ -402,28 +417,26 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
}
}
private void initWeb(File file) {
if (webp==3){
return;
}
private void initWeb(File file, int count) {
try {
count++;
WebPNative webPNative = new WebPNative();
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
webPNative.encodeRGBA(bitmap, file.getPath(), 90);
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
webp=1;
initMarkerPaper();
} catch (RuntimeException e) {
e.printStackTrace();
// 如果是写入txt记录失败上传失败记录
UMCrashManager.reportCrash(this,e);
} catch (Exception e) {
e.printStackTrace();
UMCrashManager.reportCrash(this,e);
handler.postDelayed(new Runnable() {
@Override
public void run() {
webp++;
initWeb(file);
}
}, 10);
if (count<3) { // 当尝试次数小于3次则加入转换队列尝试重新转换
UserApplication.fixedThreadPool.execute(new Jpg2WebpRunnable(/*result, */file, count));
}
}
}
@ -803,12 +816,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
}
} catch (Exception e) {
UMCrash.generateCustomLog(e, "自定义");
handler.postDelayed(new Runnable() {
@Override
public void run() {
initMarkerPaper();
}
}, 10);
}
}
@ -924,18 +931,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
if (timer == null) {
timer = new Timer();
}
timerTask = new TimerTask() {
@Override
public void run() {
if (radioPicture == 1) {
camera.takePicture();
} else {
Message message = new Message();
message.what = 0x101;
handler.sendMessage(message);
}
}
};
if (radioPicture == 1) {
timer.schedule(timerTask, 0);
} else if (radioPicture == 2) {

View File

@ -47,7 +47,7 @@ public class UserApplication extends Application {
initOkGo();
TalentLocationUtils.getInstance(this).startLocation(this);
//创建一个线程池
fixedThreadPool = Executors.newFixedThreadPool(5);
fixedThreadPool = Executors.newSingleThreadExecutor(); // 使用单线程线程池
}
public static UserApplication getUserApplication() {