gpt4 book ai didi

android - ACRA : How can I write ACRA report to file (in SD Card)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:48 27 4
gpt4 key购买 nike

我可以使用 ACRA 库 通过处理未捕获的异常来管理强制关闭错误。报告可以成功发送到谷歌文档、电子邮件和自定义网络服务。

但是我想要的..

  • 如何将报告写入文件 [例如。 sdcard/myapp/myLog.txt] ?

为什么我要这个..

  • 我的应用程序用户在发生强制关闭时可能没有互联网连接..如果是这样,那么我将错过报告,如果我将报告写入文件,那么我可以在互联网连接时发送到我的服务器连接可用。

最佳答案

我想你想要达到的效果,ACRA 已经完成了。这是我在 abd logcat 中看到的内容:

01-23 12:15:28.056: D/ACRA(614): Writing crash report file.
01-23 12:15:28.136: D/ACRA(614): Mark all pending reports as approved.
01-23 12:15:28.136: D/ACRA(614): Looking for error files in /data/data/com.ybi/files
01-23 12:15:28.136: V/ACRA(614): About to start ReportSenderWorker from #handleException
01-23 12:15:28.146: D/ACRA(614): Add user comment to null
01-23 12:15:28.146: D/ACRA(614): #checkAndSendReports - start
01-23 12:15:28.146: D/ACRA(614): Looking for error files in /data/data/com.ybi/files

ACRA 做的第一件事是在您应用的内部存储中创建一个文件报告。然后,如果您在线并且 errorreporter 已正确初始化,它会发送报告。否则,报告将保存在数据存储中(供以后发送)。

我没有查看数据,但我目前正在开发自定义记录器。因此,如果您想做与 ACRA 相同的事情,那很容易:

    ACRA.init(this);

// a custom reporter for your very own purposes
ErrorReporter.getInstance().setReportSender(new LocalReportSender(this));

然后:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.acra.ACRA;
import org.acra.CrashReportData;
import org.acra.ReportField;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

import android.content.Context;

import de.akquinet.android.androlog.Log;

public class LocalReportSender implements ReportSender {

private final Map<ReportField, String> mMapping = new HashMap<ReportField, String>() ;
private FileOutputStream crashReport = null;

public LocalReportSender(Context ctx) {
// the destination
try {
crashReport = ctx.openFileOutput("crashReport", Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
Log.e("TAG", "IO ERROR",e);
}
}

@Override
public void send(CrashReportData report) throws ReportSenderException {

final Map<String, String> finalReport = remap(report);

try {
OutputStreamWriter osw = new OutputStreamWriter(crashReport);

Set set = finalReport.entrySet();
Iterator i = set.iterator();

while (i.hasNext()) {
Map.Entry<String,String> me = (Map.Entry) i.next();
osw.write("[" + me.getKey() + "]=" + me.getValue());
}

osw.flush();
osw.close();
} catch (IOException e) {
Log.e("TAG", "IO ERROR",e);
}

}

private static boolean isNull(String aString) {
return aString == null || ACRA.NULL_VALUE.equals(aString);
}

private Map<String, String> remap(Map<ReportField, String> report) {

ReportField[] fields = ACRA.getConfig().customReportContent();
if (fields.length == 0) {
fields = ACRA.DEFAULT_REPORT_FIELDS;
}

final Map<String, String> finalReport = new HashMap<String, String>(
report.size());
for (ReportField field : fields) {
if (mMapping == null || mMapping.get(field) == null) {
finalReport.put(field.toString(), report.get(field));
} else {
finalReport.put(mMapping.get(field), report.get(field));
}
}
return finalReport;
}

}

我还没有完全测试它,但你明白了。希望对您有所帮助。

关于android - ACRA : How can I write ACRA report to file (in SD Card)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com