0

I am trying to create a temporary file in application context and want to use it to fill a logcat of application but getting a problem as error given from application that file not exist. Here is my code:

  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);
}

Here is a exception:

  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) 

I already have permission in my application:

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

Can someone tell me why this is not working?

user565
  • 871
  • 1
  • 22
  • 47
  • have you debugged your code ?. from the logcat it looks like your file is not being created. Look at these questions maybe they will be of any help https://stackoverflow.com/questions/3425906/creating-temporary-files-in-android https://stackoverflow.com/questions/12317934/what-is-the-best-way-to-create-temporary-files-on-android – Umair Aug 23 '17 at 10:35
  • Well i debug this and also printing a a command in console that showing: logcat -d > /data/data/com.mycome.myApp/cache/tempLogFile-295443322.log This seems to be file is created at this path – user565 Aug 23 '17 at 10:36
  • See my answer for the solution. – Xavier Rubio Jansana Aug 23 '17 at 10:38
  • 1
    @user565 then you are giving the wrong command for logcat to be saved in the file. Look at the answer below it's most probably the cause – Umair Aug 23 '17 at 10:40

1 Answers1

0

You're trying to execute the command "logcat -d tempfilename" with no parameters, instead of the command "logcat" with parameters "-d" and "tempfilename". The OS is looking for an executable called "logcat -d tempfilename" instead of "logcat".

See this question for a code snippet that works (question itself, not the answers).

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50