如何在 android 中创建临时文件

How to create a temporary file in android

我正在尝试在应用程序上下文中创建一个临时文件,并想用它来填充 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.call(AsyncTask.java:292)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:292) 
  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
  at android.os.AsyncTask$SerialExecutor.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" />

谁能告诉我为什么这不起作用?

您正在尝试执行不带参数的命令 "logcat -d tempfilename",而不是带参数“-d”和“tempfilename 的命令 "logcat" ”。 OS 正在寻找名为 "logcat -d tempfilename" 而不是 "logcat" 的可执行文件。

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