补充日志分享为打包压缩文件

This commit is contained in:
zlzw 2022-11-12 13:35:29 +08:00
parent 3f9cf21b8f
commit c4f3d10419
2 changed files with 58 additions and 27 deletions

View File

@ -28,15 +28,24 @@ import com.lzy.okserver.upload.UploadListener;
import com.lzy.okserver.upload.UploadTask; import com.lzy.okserver.upload.UploadTask;
import com.yunbao.common.CommonAppConfig; import com.yunbao.common.CommonAppConfig;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; 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.nio.file.Files;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* Created by cxf on 2019/6/20. * Created by cxf on 2019/6/20.
@ -77,15 +86,15 @@ public class LogUtil {
Log.i("异常上传", "pushError: 准备上传文件"); Log.i("异常上传", "pushError: 准备上传文件");
String err = FileUtil.loadStringToFile(file); String err = FileUtil.loadStringToFile(file);
String time = matcher(".(?<=time\\=).*(?=\\AndroidVersion\\=)", err); String time = matcher(".(?<=time\\=).*(?=\\AndroidVersion\\=)", err);
if(time!=null){ if (time != null) {
time=time.replace("=",""); time = time.replace("=", "");
}else{ } else {
time=System.currentTimeMillis()+""; time = System.currentTimeMillis() + "";
} }
PostRequest<String> request = OkGo.<String>post("https://newht.yaoulive.com/index.php/api/error_log/report") PostRequest<String> request = OkGo.<String>post("https://newht.yaoulive.com/index.php/api/error_log/report")
.params("log", err) .params("log", err)
.params("uid", CommonAppConfig.getInstance().getUid()) .params("uid", CommonAppConfig.getInstance().getUid())
.params("time",time) .params("time", time)
.params("isGoogle", CommonAppConfig.IS_GOOGLE_PLAY) .params("isGoogle", CommonAppConfig.IS_GOOGLE_PLAY)
.params("appVersion", CommonAppConfig.getInstance().getVersion()) .params("appVersion", CommonAppConfig.getInstance().getVersion())
.tag("errorLog") .tag("errorLog")
@ -141,42 +150,64 @@ public class LogUtil {
public static void shareFile(Context context) { public static void shareFile(Context context) {
if(PermissionChecker.checkCallingOrSelfPermission(context,"android.permission.WRITE_EXTERNAL_STORAGE")!= PERMISSION_GRANTED){ if (PermissionChecker.checkCallingOrSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100); ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
return; return;
} }
String today ="[PDLIVE]"+CommonAppConfig.getInstance().getUid()+"_"+ SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date()); String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_";
File file = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log"); String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
if (file.exists()) { 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); Intent share = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 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.putExtra(Intent.EXTRA_STREAM, contentUri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else { } else {
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(zip));
} }
share.setType("application/vnd.ms-excel"); share.setType("application/vnd.ms-excel");
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "分享文件")); context.startActivity(Intent.createChooser(share, "分享文件"));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if(!Environment.isExternalStorageManager()){ if (!Environment.isExternalStorageManager()) {
Intent intent=new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); Intent intent = new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
context.startActivity(intent); context.startActivity(intent);
} }
} }
File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + file.getName()); File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + zip.getName());
file.renameTo(out);
Log.i("文件", "shareFile: "+out.getAbsolutePath()+" "+out.exists());
try { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.copy(file.toPath(),out.toPath()); Files.copy(zip.toPath(), out.toPath());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
} }