Skip to content

Commit

Permalink
修复,崩溃问题,去掉存储日志文件,并改写存储文件的方式,使用最新的api获取文件目录
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolls committed Jan 9, 2017
1 parent 7431aca commit 35e59ec
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 74 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "0.0.9"
versionName "0.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.egovcomm.monitor.R;
import com.egovcomm.monitor.adapter.EBBaseAdapter;
import com.egovcomm.monitor.adapter.GroupListAdapter;
import com.egovcomm.monitor.common.BaseApplication;
import com.egovcomm.monitor.db.DBHelper;
import com.egovcomm.monitor.model.ItemEntity;
import com.egovcomm.monitor.model.MonitorMedia;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void dataBack(int id, Object obj) {
&& group.getData().getData() != null) {
List<RspMediaGroup> listGroup = group.getData().getData();
for(RspMediaGroup g:listGroup){
g.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath()+File.separator+g.getId()+".jpg");//用服务器回来的ID做缩略图
g.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath(GroupMediaListActivity.this)+File.separator+g.getId()+".jpg");//用服务器回来的ID做缩略图
}
list.addAll(listGroup);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
toastTime =System.currentTimeMillis();
requestTime =System.currentTimeMillis();
LogUtils.writeLogtoFile("mjk:","MainUserActivity--onCreate");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","MainUserActivity--onCreate");
startLocation();//开始定位
}

Expand All @@ -83,7 +83,7 @@ public void onData(View view) {

public void onProfile(View view) {
// ToastUtils.toast(getApplicationContext(), "个人中心");
LogUtils.writeLogtoFile("mjk:","MainUserActivity-打开profile页面");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","MainUserActivity-打开profile页面");
openActivity(ProfileActivity.class,null,false,true,ProfileActivity.REQUEST_CODE_PROFILE);

}
Expand All @@ -92,20 +92,20 @@ public void onProfile(View view) {
@Override
protected void onDestroy() {
super.onDestroy();
LogUtils.writeLogtoFile("mjk:","执行MainUser 中的destroy中的exitApp方法");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","执行MainUser 中的destroy中的exitApp方法");
exitApp();
LogUtils.writeLogtoFile("mjk:","MainUserActivity--onDestroy结束");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","MainUserActivity--onDestroy结束");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtils.writeLogtoFile("mjk:","MainUserActivity--onActivityResult关闭profile返回");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","MainUserActivity--onActivityResult关闭profile返回");
if(requestCode==ProfileActivity.REQUEST_CODE_PROFILE&&resultCode==RESULT_OK){//需要注销登录
openActivity(SigninActivity.class,null,true);
//以下为测试代码
// ToastUtils.toast(MainUserActivity.this,"延迟后打开登录页面");
// LogUtils.writeLogtoFile("mjk:","发送延迟Handler");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","发送延迟Handler");
// handler.sendEmptyMessageDelayed(0,2000);
}
}
Expand All @@ -114,32 +114,32 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
LogUtils.writeLogtoFile("mjk:","收到延迟Handler并打开登录销毁MainUser页面");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","收到延迟Handler并打开登录销毁MainUser页面");
openActivity(SigninActivity.class,null,true);
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime) > AppConstant.SPACE_TIME){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime) > AppConstant.SPACE_TIME){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
LogUtils.writeLogtoFile("mjk:","按两次返回键");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","按两次返回键");
finish();
}
return true;
return true;
}
return super.onKeyDown(keyCode, event);
}

/**注销退出应用*/
private void exitApp(){
LogUtils.writeLogtoFile("mjk:","进入exitApp方法");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","进入exitApp方法");
BaseApplication.status = BaseApplication.STATUS_OFFLINE;
LogUtils.writeLogtoFile("mjk:","发送最后一次位置");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","发送最后一次位置");
uploadLocation();// 结束的时候发送最后一次位置
LogUtils.writeLogtoFile("jk:","开始执行destroyLocation");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","开始执行destroyLocation");
try {
new Thread(){

Expand All @@ -152,7 +152,7 @@ public void run() {
}catch (Exception e){
e.printStackTrace();
}
LogUtils.writeLogtoFile("jk:","执行destroyLocation完成");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","执行destroyLocation完成");
}

/**
Expand All @@ -165,7 +165,7 @@ public void run() {
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);// 可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);// 可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setGpsFirst(true);// 可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setHttpTimeOut(30000);// 可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(3000);// 可选,设置定位间隔。默认为3秒
mOption.setNeedAddress(true);// 可选,设置是否返回逆地理地址信息。默认是true
Expand Down Expand Up @@ -213,7 +213,7 @@ public void onLocationChanged(AMapLocation loc) {
*
*/
private void startLocation() {
LogUtils.writeLogtoFile("mjk:","开始定位");
// LogUtils.writeLogtoFile(MainUserActivity.this,"mjk:","开始定位");
// 初始化client
locationClient = new AMapLocationClient(this.getApplicationContext());
// 设置定位参数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,34 @@ private void freshData(){
private boolean isNeedDownLoad(MonitorMedia media) {
boolean isNeed = false;
// 只有是服务器的并且还没有下载的才去请求
if (media.getDownloadState()!=MonitorMedia.DOWNLOAD_STATE_DOWNLOADING&&!FileUtils.isFileExit(FileUtils.getAppStorageOriginalDirectoryPath()
if (media.getDownloadState()!=MonitorMedia.DOWNLOAD_STATE_DOWNLOADING&&!FileUtils.isFileExit(FileUtils.getAppStorageOriginalDirectoryPath(MediaListActivity.this)
+ File.separator + media.getFileName())) {// 通过本地服务器路径加上文件名来判断文件是否存在
isNeed = true;
} else {
isNeed = false;
media.setPath(FileUtils.getAppStorageOriginalDirectoryPath()
media.setPath(FileUtils.getAppStorageOriginalDirectoryPath(MediaListActivity.this)
+ File.separator + media.getFileName());
media.setDownloadState(MonitorMedia.DOWNLOAD_STATE_DOWNLOADED);


if (!FileUtils.isFileExit(FileUtils
.getAppStorageThumbnailDirectoryPath()
.getAppStorageThumbnailDirectoryPath(MediaListActivity.this)
+ File.separator + media.getFileName())) {
// 缩略图不存在,则存储一个缩略图
String thump = FileUtils.saveMediaThumbnail(
getApplicationContext(),
FileUtils.getAppStorageOriginalDirectoryPath()
FileUtils.getAppStorageOriginalDirectoryPath(MediaListActivity.this)
+ File.separator + media.getFileName(),
media.getMediaType(), false);
media.setThumbnailPath(thump);
} else {
media.setThumbnailPath(FileUtils
.getAppStorageThumbnailDirectoryPath()
.getAppStorageThumbnailDirectoryPath(MediaListActivity.this)
+ File.separator + media.getFileName());
}

/**在这里添加如果组没有缩略图则创建一个*/
if(uploadGroup!=null&&!FileUtils.isFileExit(FileUtils.getAppStorageThumbnailDirectoryPath()+File.separator+uploadGroup.getId()+".jpg")) {
if(uploadGroup!=null&&!FileUtils.isFileExit(FileUtils.getAppStorageThumbnailDirectoryPath(MediaListActivity.this)+File.separator+uploadGroup.getId()+".jpg")) {
//不存在
FileUtils.saveMediaGroupThumbnail(this,media.getThumbnailPath(),uploadGroup.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void onShutter() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
File file=CameraHelper.saveData(data, CameraHelper.MEDIA_TYPE_IMAGE, CameraHelper.SCREEN_PORTRAIT);
File file=CameraHelper.saveData(PhotoCaptureActivity.this,data, CameraHelper.MEDIA_TYPE_IMAGE, CameraHelper.SCREEN_PORTRAIT);
String thumpFilePath=FileUtils.saveMediaThumbnail(getApplicationContext(), file.getAbsolutePath(), MonitorMediaGroup.TYPE_PHOTO+"",true);
mCamera.stopPreview();
mCamera.startPreview();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
mBack.setOnClickListener(this);
mRightTv.setOnClickListener(this);
mRightIv.setOnClickListener(this);
LogUtils.writeLogtoFile("mjk:","Profile onCreate");
// LogUtils.writeLogtoFile(ProfileActivity.this,"mjk:","Profile onCreate");

initData();
}
Expand Down Expand Up @@ -88,15 +88,15 @@ public void onCheckUpdate(View view){

public void onExit(View view){
SPUtils.cleanLocalData(this);
LogUtils.writeLogtoFile("mjk:","Profile 点击了注销登录按钮");
// LogUtils.writeLogtoFile(ProfileActivity.this,"mjk:","Profile 点击了注销登录按钮");
setResult(RESULT_OK);
finish();
}

@Override
protected void onDestroy() {
super.onDestroy();
LogUtils.writeLogtoFile("mjk:","Profile onDestroy");
// LogUtils.writeLogtoFile(ProfileActivity.this,"mjk:","Profile onDestroy");
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/egovcomm/monitor/common/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.egovcomm.monitor.ftp.FTPConnection;
import com.egovcomm.monitor.model.AppRequest;
import com.egovcomm.monitor.utils.FileUtils;
import com.egovcomm.monitor.utils.LogUtils;
import com.egovcomm.monitor.utils.SPUtils;
import com.egovcomm.monitor.utils.ToastUtils;

import android.app.Application;
import android.text.TextUtils;

/**
* Application基类
Expand All @@ -28,6 +31,7 @@ public void onCreate() {
initData();
}


private void initData(){
isUpdating=false;
LogUtils.i("BaseApplication", "初始化数据!!!!!");
Expand All @@ -43,6 +47,15 @@ private void initData(){
FTPConnection.port=ftpPort;
FTPConnection.userName=ftpUserName;
FTPConnection.password=ftpPassword;

//创建默认文件夹
String originalPath=FileUtils.getAppStorageOriginalDirectoryPath(this);
String thumbnailPath=FileUtils.getAppStorageThumbnailDirectoryPath(this);
LogUtils.i("application","初始化application-源目录:"+originalPath);
LogUtils.i("application","初始化application-缩略图目录:"+thumbnailPath);
if(TextUtils.isEmpty(originalPath)||TextUtils.isEmpty(thumbnailPath)){
ToastUtils.toast(this,"存储不可用,请检查设备状态!");
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/egovcomm/monitor/db/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DBHelper extends SQLiteOpenHelper {
private final static String TAG = DBHelper.class.getSimpleName();
private static DBHelper _instance;
private static final String DB_NAME = "monitor.db";
private static final int DB_VERSION = 2;
private static final int DB_VERSION = 3;
private static Context mContext;
private static Gson gson = new Gson();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void dataBack(int id, Object obj) {
MonitorMediaGroupUpload group = it.next();
group.setShowCheck(0);
group.setCheck(0);
group.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath()+File.separator+group.getId()+".jpg");//用组ID做缩略图
group.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath(getActivity())+File.separator+group.getId()+".jpg");//用组ID做缩略图
if (!TextUtils.isEmpty(mediaType)&&!TextUtils.equals(mediaType, group.getMediaGroup()
.getMediaType())) {
it.remove();
Expand All @@ -96,7 +96,7 @@ public void dataBack(int id, Object obj) {
for(RspMediaGroup g:listGroup){
MonitorMediaGroupUpload mg=new MonitorMediaGroupUpload();
mg.setId(g.getId());
mg.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath()+File.separator+g.getId()+".jpg");//用服务器回来的ID做缩略图
mg.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath(getActivity())+File.separator+g.getId()+".jpg");//用服务器回来的ID做缩略图
mg.setMediaGroup(g);
mg.setUploadState(MonitorMediaGroupUpload.UPLOAD_STATE_SERVER_DATA);
mg.setShowCheck(0);
Expand All @@ -110,7 +110,7 @@ public void dataBack(int id, Object obj) {
mediaList.add(media);
if(getActivity()!=null&&!FileUtils.isFileExit(mg.getThumbnailPath())){//组缩略图不存在,寻找子有没有
media.setThumbnailPath(FileUtils
.getAppStorageThumbnailDirectoryPath()
.getAppStorageThumbnailDirectoryPath(getActivity())
+ File.separator + media.getFileName());
if (FileUtils.isFileExit(media.getThumbnailPath())) {//子缩略图存在
FileUtils.saveMediaGroupThumbnail(getActivity(),media.getThumbnailPath(),g.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void listViewRefresh() {
Iterator<MonitorMediaGroupUpload> it = list.iterator();
while (it.hasNext()) {
MonitorMediaGroupUpload group = it.next();
group.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath()+File.separator+group.getId()+".jpg");//用组ID做缩略图
group.setThumbnailPath(FileUtils.getAppStorageThumbnailDirectoryPath(getActivity())+File.separator+group.getId()+".jpg");//用组ID做缩略图

if (!TextUtils.isEmpty(mediaType)&&!TextUtils.equals(mediaType, group.getMediaGroup().getMediaType())) {
it.remove();
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/egovcomm/monitor/utils/CameraHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ protected MediaRecorder doInBackground(Void... params) {


// Step 4: Set output file
path=getOutputMediaFile(
path=getOutputMediaFile(context,
CameraHelper.MEDIA_TYPE_VIDEO).toString();
mMediaRecorder.setOutputFile(path);
// END_INCLUDE (configure_media_recorder)
Expand Down Expand Up @@ -560,8 +560,8 @@ private static Camera getDefaultCamera(int position) {
* Media type. Can be video or image.
* @return A file object pointing to the newly created file.
*/
public static File getOutputMediaFile(int type) {
File mediaStorageDir=new File(FileUtils.getAppStorageOriginalDirectoryPath());
public static File getOutputMediaFile(Context context,int type) {
File mediaStorageDir=new File(FileUtils.getAppStorageOriginalDirectoryPath(context));
// Create a media file name
//String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String timeStamp =UUID.randomUUID().toString();
Expand All @@ -577,10 +577,10 @@ public static File getOutputMediaFile(int type) {
return mediaFile;
}

public static File saveData(byte[] data,int mediaType,int screenOrientation){
public static File saveData(Context context,byte[] data,int mediaType,int screenOrientation){
String path="";
File saveFile=null;
saveFile=getOutputMediaFile(mediaType);
saveFile=getOutputMediaFile(context,mediaType);
path=saveFile.getAbsolutePath();
if(mediaType==MEDIA_TYPE_IMAGE){
Bitmap b = null;
Expand Down
Loading

0 comments on commit 35e59ec

Please sign in to comment.