简体   繁体   中英

How to create a temporary file in android

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM