From c4f3d10419c67cbb9b2b8f243d9140c13d31839e Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 12 Nov 2022 13:35:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=97=A5=E5=BF=97=E5=88=86?= =?UTF-8?q?=E4=BA=AB=E4=B8=BA=E6=89=93=E5=8C=85=E5=8E=8B=E7=BC=A9=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shayu/phonelive/AppContext.java | 2 +- .../java/com/yunbao/common/utils/LogUtil.java | 83 +++++++++++++------ 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/shayu/phonelive/AppContext.java b/app/src/main/java/com/shayu/phonelive/AppContext.java index 5fccbeebf..e1ddc7225 100644 --- a/app/src/main/java/com/shayu/phonelive/AppContext.java +++ b/app/src/main/java/com/shayu/phonelive/AppContext.java @@ -274,7 +274,7 @@ public class AppContext extends CommonAppContext { }); configSPApp(); //初始化美颜SDK - // FaceManager.initFaceUnity(this); + // FaceManager.initFaceUnity(this); } /** diff --git a/common/src/main/java/com/yunbao/common/utils/LogUtil.java b/common/src/main/java/com/yunbao/common/utils/LogUtil.java index 128ab9f74..47c4c8ea7 100644 --- a/common/src/main/java/com/yunbao/common/utils/LogUtil.java +++ b/common/src/main/java/com/yunbao/common/utils/LogUtil.java @@ -28,15 +28,24 @@ import com.lzy.okserver.upload.UploadListener; import com.lzy.okserver.upload.UploadTask; import com.yunbao.common.CommonAppConfig; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.WritableByteChannel; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** * Created by cxf on 2019/6/20. @@ -77,15 +86,15 @@ public class LogUtil { Log.i("异常上传", "pushError: 准备上传文件"); String err = FileUtil.loadStringToFile(file); String time = matcher(".(?<=time\\=).*(?=\\AndroidVersion\\=)", err); - if(time!=null){ - time=time.replace("=",""); - }else{ - time=System.currentTimeMillis()+""; + if (time != null) { + time = time.replace("=", ""); + } else { + time = System.currentTimeMillis() + ""; } PostRequest request = OkGo.post("https://newht.yaoulive.com/index.php/api/error_log/report") .params("log", err) .params("uid", CommonAppConfig.getInstance().getUid()) - .params("time",time) + .params("time", time) .params("isGoogle", CommonAppConfig.IS_GOOGLE_PLAY) .params("appVersion", CommonAppConfig.getInstance().getVersion()) .tag("errorLog") @@ -141,41 +150,63 @@ public class LogUtil { public static void shareFile(Context context) { - if(PermissionChecker.checkCallingOrSelfPermission(context,"android.permission.WRITE_EXTERNAL_STORAGE")!= PERMISSION_GRANTED){ - ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100); + if (PermissionChecker.checkCallingOrSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) { + ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); return; } - String today ="[PDLIVE]"+CommonAppConfig.getInstance().getUid()+"_"+ SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date()); - File file = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log"); - if (file.exists()) { + String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_"; + String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date()); + File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator); + File zip = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".zip"); + + try { + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip))); + FileInputStream fis = null; + WritableByteChannel writableByteChannel = Channels.newChannel(zos); + if (dir.listFiles() != null) { + for (File file : dir.listFiles()) { + if (file.getName().endsWith(".log") && !"error.log".equals(file.getName())) { + ZipEntry entry = new ZipEntry(file.getName()); + zos.putNextEntry(entry); + fis = new FileInputStream(file); + FileChannel channel = fis.getChannel(); + channel.transferTo(0, file.length() - 1, writableByteChannel); + fis.close(); + } + } + } + zos.close(); + writableByteChannel.close(); + } catch (Exception e) { + e.printStackTrace(); + } + if (zip.exists()) { Intent share = new Intent(Intent.ACTION_SEND); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); + Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", zip); share.putExtra(Intent.EXTRA_STREAM, contentUri); share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { - share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); + share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(zip)); } share.setType("application/vnd.ms-excel"); share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser(share, "分享文件")); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - if(!Environment.isExternalStorageManager()){ - Intent intent=new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); - context.startActivity(intent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + if (!Environment.isExternalStorageManager()) { + Intent intent = new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); + context.startActivity(intent); + } } - } - File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + file.getName()); - file.renameTo(out); - Log.i("文件", "shareFile: "+out.getAbsolutePath()+" "+out.exists()); - try { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Files.copy(file.toPath(),out.toPath()); + File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + zip.getName()); + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Files.copy(zip.toPath(), out.toPath()); + } + } catch (Exception e) { + e.printStackTrace(); } - } catch (Exception e) { - e.printStackTrace(); } }