如何在android

时间:2017-08-23 10:29:45

标签: java android android-logcat

我正在尝试在应用程序上下文中创建一个临时文件,并希望使用它来填充应用程序的logcat,但是从应用程序提供的错误中获取问题该文件不存在。这是我的代码:

  try {
  String command="logcat -d";
  final File outputFile = File.createTempFile("tempLogFile", ".log", context.getCacheDir());
  ProcessBuilder p = new ProcessBuilder(command + " > " + outputFile.getAbsolutePath());
  p.start().waitFor();

  AsyncHttpPut put = new AsyncHttpPut(url);
  MultipartFormDataBody body = new MultipartFormDataBody();
  body.addFilePart("file", outputFile);
  put.setBody(body);

  AsyncHttpClient.getDefaultInstance().executeString(put, new AsyncHttpClient.StringCallback() {
      @Override
      public void onCompleted(Exception ex, AsyncHttpResponse asyncHttpResponse, String res) {
          outputFile.delete();
          if (ex != null) {
              Log.e(TAG, " Error to update log file to server", ex);
              return;
          }

      }
  });

            } catch (IOException e) {
  Log.e(TAG, "Cannot execute log command:" + command, e);
            } catch (InterruptedException e) {
  Log.e(TAG, "Interrupted while calling a command " + command, e);
}

这是一个例外:

  08-23 12:16:43.558 27173-27976/com.mycome.myApp E/Util: Cannot execute log command:logcat -d
  java.io.IOException: Error running exec(). Command: [logcat -d > /data/data/com.mycome.myApp/cache/tempLogFile-295443322.log] Working Directory: null Environment: [ANDROID_ROOT=/system, EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, LOOP_MOUNTPOINT=/mnt/obb, LD_PRELOAD=libsigchain.so:libNimsWrap.so, ANDROID_BOOTLOGO=1, EMULATED_STORAGE_TARGET=/storage/emulated, EXTERNAL_STORAGE=/storage/emulated/legacy, SYSTEMSERVERCLASSPATH=/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar, ANDROID_SOCKET_zygote=9, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, BOOTCLASSPATH=/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar:/system/framework/dolby_ds.jar:/system/framework/dolby_ds2.jar:/system/framework/qcmediaplayer.jar:/system/framework/WfdCommon.jar:/system/framework/oem-services.jar:/system/framework/qcom.fmradio.jar:/system/framework/org.codeaurora.Performance.jar:/system/framework/vcard.jar:/system/framework/tcmiface.jar, ANDROID_PROPERTY_WORKSPACE=8,0, SECONDARY_STORAGE=/storage/sdcard1, ANDROID_STORAGE=/storage]
  at java.lang.ProcessManager.exec(ProcessManager.java:211)
  at java.lang.ProcessBuilder.start(ProcessBuilder.java:195)
  at com.mycome.myApp.util.Util$LongOperation.doInBackground(Util.java:232)
  at com.mycome.myApp.util.Util$LongOperation.doInBackground(Util.java:215)
  at android.os.AsyncTask$2.call(AsyncTask.java:292)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:818)
   Caused by: java.io.IOException: No such file or directory
  at java.lang.ProcessManager.exec(Native Method)
  at java.lang.ProcessManager.exec(ProcessManager.java:209)
  at java.lang.ProcessBuilder.start(ProcessBuilder.java:195) 
  at com.mycome.myApp.util.Util$LongOperation.doInBackground(Util.java:232) 
  at com.mycome.myApp.util.Util$LongOperation.doInBackground(Util.java:215) 
  at android.os.AsyncTask$2.call(AsyncTask.java:292) 
  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
  at java.lang.Thread.run(Thread.java:818) 

我的申请已获得许可:

  <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

有人可以告诉我为什么这不起作用吗?

1 个答案:

答案 0 :(得分:0)

您正在尝试执行命令&#34; logcat -d tempfilename &#34;没有参数,而不是命令&#34; logcat&#34;参数&#34; -d&#34;和&#34; tempfilename &#34;。操作系统正在寻找名为&#34; logcat -d tempfilename &#34;的可执行文件。而不是&#34; logcat&#34;。

请参阅this question了解有效的代码段(问题本身,而不是答案)。