ACRA 4.9.0:如何将 ACRA 报告写入文件(在应用程序数据文件夹中)

ACRA 4.9.0 : How can I write ACRA report to file (in Application data folder)

我想使用最新的 Acra 4.9.0 在文本文件中编写崩溃报告。 我不能举例说明这个最新版本。 我尝试使用可用的文档。

Acra 已启用 但是它没有写入文件。

myApp

 package com.myApp;

import org.acra.ACRA;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;

import com.myApp.Application.AppLauncher;
import com.myApp.interfaces.AppEvents;
import com.myApp.R;
import com.utils.Config;
import com.utils.Constants;
import com.utils.DeviceValidator;

public class myApp extends FragmentActivity
{
    private AppLauncher appLauncher = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        if(!ACRA.isACRASenderServiceProcess())
        {
            setContentView(R.layout.activity_myApp);

            appLauncher = new AppLauncher();

            appLauncher.registerEventListener(appEventsListener);

            appLauncher.initApp(this);

        }
    }

    @Override
    public void onPause() {
        super.onPause();

        if(!DeviceValidator.isDeviceValid())
        {
            return;
        }

        appLauncher.activityOnPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() 
    {
        super.onStart();
    }

    @Override
    public void onResume()
    {
        super.onResume();

        appLauncher.activityOnResume();
    }
}

AcraApplication

    package com.myAPP;

    import org.acra.ACRA;
    import org.acra.ReportField;
    import org.acra.ReportingInteractionMode;
    import org.acra.annotation.ReportsCrashes;
    import org.acra.sender.HttpSender.Method;

    import android.app.Application;

    @ReportsCrashes(
            formUri = "http://staging.jemtv.com/variable_dump/index.php",
            customReportContent = { ReportField.REPORT_ID, ReportField.DEVICE_ID, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.STACK_TRACE, ReportField.CUSTOM_DATA, ReportField.LOGCAT },
            httpMethod = Method.POST,
            reportSenderFactoryClasses = org.acra.util.MyOwnSenderFactory.class,
            mode = ReportingInteractionMode.SILENT
    )

    public class AcraApplication extends Application
    {
        public AcraApplication() 
        {
            super();
        }

        @Override
        public void onCreate() {
            super.onCreate();
            ACRA.init(this);
        }

    }

MyOwnSender

package org.acra.util;

import java.io.File;
import java.io.FileOutputStream;

import org.acra.ReportField;
import org.acra.collector.CrashReportData;
import org.acra.config.ACRAConfiguration;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

public class MyOwnSender implements ReportSender  {
    private static final String FILE_NAME = "AcraReport.txt";
    //private final Map<ReportField, String> mMapping = new HashMap<ReportField, String>();
    //private FileWriter crashReport = null;

    MyOwnSender(Context context,  @NonNull ACRAConfiguration config)
    {
        Log.d("testAcra", "MyOwnSender created");
       /* File logFile = new File(context.getFilesDir().getPath() + "/" + FILE_NAME, FILE_NAME);

        try {
            crashReport = new FileWriter(logFile, true);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @Override
    public void send(Context context, CrashReportData report) throws ReportSenderException
    {
            // Iterate over the CrashReportData instance and do whatever
            // you need with each pair of ReportField key / String value

         String finalReport = createCrashReport(report);
         String tempFile = context.getFilesDir().getPath() + "/" + FILE_NAME;

        try
        {            
            File detailedFile = new File(tempFile); 

            if(!detailedFile.exists())
                detailedFile.createNewFile();

            FileOutputStream stream = new FileOutputStream(detailedFile, true);

            stream.write(finalReport.getBytes());
            Log.d("testAcra","adding to file: "+stream);
            stream.close();

           }
       catch (Exception e)
       { 

        e.printStackTrace(); 
       }
     /*final Map<String, String> finalReport = remap(report);

         try {
             BufferedWriter buf = new BufferedWriter(crashReport);

             Set<Entry<String, String>> set = reportBody.entrySet();
             Iterator<Entry<String, String>> i = set.iterator();

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

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

    private String createCrashReport(CrashReportData crashReportData){
        StringBuilder body = new StringBuilder();

        body.append("ReportID : " + crashReportData.getProperty(ReportField.REPORT_ID))
                .append("\n")
                .append("DeviceID : " + crashReportData.getProperty(ReportField.DEVICE_ID))
                .append("\n")
                .append("AppVersionName : " + crashReportData.getProperty(ReportField.APP_VERSION_NAME))
                .append("\n")
                .append("Android Version : " + crashReportData.getProperty(ReportField.ANDROID_VERSION))
                .append("\n")
                .append("CustomData : " + crashReportData.getProperty(ReportField.CUSTOM_DATA))
                .append("\n")
                .append("STACK TRACE : \n" + crashReportData.getProperty(ReportField.STACK_TRACE))
                .append("\n")
                .append("LogCAT : \n" + crashReportData.getProperty(ReportField.LOGCAT));

        return body.toString();
    }

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

       Set<ReportField>fields = ACRA.getConfig().getReportFields();


        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;
    }*/
}

MyOwnSenderFactory

package org.acra.util;

import org.acra.config.ACRAConfiguration;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderFactory;

import android.content.Context;
import android.support.annotation.NonNull;

public class MyOwnSenderFactory implements ReportSenderFactory {

     // NB requires a no arg constructor.
    /*MyOwnSenderFactory()
    {
         Log.e("testAcra", "MyOwnSenderFactory created");
    }*/

    @Override
    @NonNull
    public ReportSender create(@NonNull Context context, @NonNull ACRAConfiguration config) {
        // TODO Auto-generated method stub
        return new MyOwnSender(context, config);
    }
}

I want to write in application data folder

那叫internal data

我已经创建了自己的储蓄器 class 来处理所有这些储蓄:

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;

public class Saver {


    static FileOutputStream fos;

    static FileInputStream fis;


    public static void save(String filename, String data, Context c){




            try {

                fos = c.openFileOutput(filename, Context.MODE_PRIVATE);

                fos.write(data.getBytes());
                fos.close();

            } catch (Exception e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }



    }



    String file;
    public String getFile(){return file;}

    public void setFile(String loc){
        file = loc;
    }

    String result;
    private static String mainLoad(String fn, Context c){
        String collected = null;
        try{

            fis = c.openFileInput(fn);
            byte[] dataArray = new byte[fis.available()];

            while(fis.read(dataArray) != -1){
                collected = new String(dataArray);
            }


        }catch(Exception e){
            e.printStackTrace();
            return null;

        }finally{
            try {
                fis.close();

                return collected;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }


    }

    public static int loadInt(String fn, Context c){
        if(mainLoad(fn,c) == null) return 0;
        else return Integer.parseInt(mainLoad(fn,c));
    }
    public static double loadDouble(String fn, Context c){
        if(mainLoad(fn,c) == null) return 0;
        else return Double.parseDouble(mainLoad(fn,c));
    }

    public static float loadFloat(String fn, Context c){
        return Float.parseFloat(mainLoad(fn,c));
    }

    public static String loadString(String fn, Context c){
        return mainLoad(fn, c);
    }

    public static Boolean loadBoolean(String fn, Context c){
        if(mainLoad(fn,c) == null) return false;
        else return Boolean.parseBoolean(mainLoad(fn,c));
    }

    public static BigInteger loadBigInteger(String fn, Context c){

        return new BigInteger(mainLoad(fn,c));
    }

    public static BigDecimal loadBigDecimal(String fn, Context c){
        return new BigDecimal(mainLoad(fn,c));
    }

}

I want to write the crash report in text file using latest Acra 4.9.0. I can,t example for this latest version.

如果您想写入服务器上的 .txt 文件,请尝试 this backend。使用默认发件人:

<?php
    // Outputs all POST parameters to a text file. The file name is the date_time of the report reception
    $fileName = date('Y-m-d_H-i-s').'.txt';
    $file = fopen($fileName,'w') or die('Could not create report file: ' . $fileName);
    foreach($_POST as $key => $value) {
    $reportLine = $key." = ".$value."\n";
        fwrite($file, $reportLine) or die ('Could not write to report file ' . $reportLine);
    }
    fclose($file);
?>

如果你只想在本地写,那么ACRA的意义就没有了,因为你拿不到文件

如果你创建.txt 文件来传输它们,实际上使用我链接的后端更好。它传输原始数据,您可以在 .txt 文件中获取所有字段

因为我使用的是 jar 文件而不是 aar 在我的清单中我丢失了

 <service
            android:name="org.acra.sender.SenderService"
            android:exported="false"
            android:process=":acra" />
enter code here

这就是为什么在 Acra 中使用的 SendeService 没有启动的原因。