我试图在Android中创建一个.csv文件,以便通过电子邮件/其他方法共享它们,但是当我尝试创建临时文件时,我收到以下错误:
无法创建临时文件/data/user/0/com.example.froggy.DemoApp/cache/dir/activetickets2582149955186383020.csv
以下是我的代码部分:
File file = File.createTempFile("dir/activetickets", ".csv", getApplicationContext().getCacheDir());
FileWriter fw = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fw);
for(Ticket t : tickets){
writer.write(t.tag + "," + t.date.getTime().toString() + "," + t.details + "\n");
}
writer.close();
fw.close();
编辑:这是完整的logcat
04-01 14:18:55.060 30030-30030/? I/zygote64: Late-enabling -Xcheck:jni
04-01 14:18:55.100 30030-30038/? I/zygote64: Debugger is no longer active
04-01 14:18:55.135 30030-30030/? I/MultiDex: VM with version 2.1.0 has multidex support
04-01 14:18:55.135 30030-30030/? I/MultiDex: Installing application
04-01 14:18:55.135 30030-30030/? I/MultiDex: VM has multidex support, MultiDex support library is disabled.
04-01 14:18:55.157 30030-30030/? W/zygote64: Skipping duplicate class check due to unrecognized classloader
04-01 14:18:55.160 30030-30030/? W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
04-01 14:18:55.163 30030-30030/? W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
04-01 14:18:55.166 30030-30030/? I/BiChannelGoogleApi: [FirebaseAuth: ] No Fallback module; NOT setting up for lazy initialization
04-01 14:18:55.169 30030-30063/? W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
04-01 14:18:55.179 30030-30063/? I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
04-01 14:18:55.179 30030-30063/? I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
04-01 14:18:55.191 30030-30069/? I/DynamiteModule: Considering local module com.google.android.gms.flags:2 and remote module com.google.android.gms.flags:0
04-01 14:18:55.191 30030-30069/? I/DynamiteModule: Selected local version of com.google.android.gms.flags
04-01 14:18:55.195 30030-30030/? V/FA: Cancelling job. JobID: 2054842230
04-01 14:18:55.196 30030-30030/? V/FA: Registered activity lifecycle callback
04-01 14:18:55.197 30030-30030/? I/FirebaseInitProvider: FirebaseApp initialization successful
04-01 14:18:55.210 30030-30069/? W/DynamiteModule: Local module descriptor class for com.google.android.gms.crash not found.
04-01 14:18:55.213 30030-30069/? I/DynamiteModule: Considering local module com.google.android.gms.crash:0 and remote module com.google.android.gms.crash:213
04-01 14:18:55.213 30030-30069/? I/DynamiteModule: Selected remote version of com.google.android.gms.crash, version >= 213
04-01 14:18:55.216 30030-30072/? V/FA: Collection enabled
04-01 14:18:55.217 30030-30072/? V/FA: App package, google app id: com.example.froggy.Demo21C, 1:191817514314:android:429c490d2f7c4707
04-01 14:18:55.218 30030-30072/? I/FA: App measurement is starting up, version: 11910
04-01 14:18:55.218 30030-30072/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
04-01 14:18:55.218 30030-30072/? I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.froggy.Demo21C
04-01 14:18:55.218 30030-30072/? D/FA: Debug-level message logging enabled
04-01 14:18:55.223 30030-30030/? V/FA: onActivityCreated
04-01 14:18:55.233 30030-30072/? V/FA: Connecting to remote service
04-01 14:18:55.239 30030-30072/? V/FA: Connection attempt already in progress
04-01 14:18:55.241 30030-30069/? W/zygote64: Skipping duplicate class check due to unrecognized classloader
04-01 14:18:55.253 30030-30069/? I/FirebaseCrashApiImpl: FirebaseCrashApiImpl created by ClassLoader ae[DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/00000037/DynamiteModulesC_GmsCore_prodmnc_alldpi_release.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000037/n/arm64-v8a, /system/lib64, /system/vendor/lib64]]]
04-01 14:18:55.253 30030-30069/? I/FirebaseCrash: FirebaseCrash reporting loaded - com.google.android.gms.internal.zzdzk@ce3f80f
04-01 14:18:55.264 30030-30070/? I/DynamiteModule: Considering local module com.google.android.gms.flags:2 and remote module com.google.android.gms.flags:0
04-01 14:18:55.264 30030-30070/? I/DynamiteModule: Selected local version of com.google.android.gms.flags
04-01 14:18:55.276 30030-30070/? W/DynamiteModule: Local module descriptor class for com.google.android.gms.crash not found.
04-01 14:18:55.313 30030-30072/? V/FA: Connection attempt already in progress
04-01 14:18:55.313 30030-30070/? I/FirebaseCrashApiImpl: FirebaseCrash reporting API initialized
04-01 14:18:55.314 30030-30072/? V/FA: Activity resumed, time: 1234534715
04-01 14:18:55.315 30030-30070/? I/FirebaseCrash: FirebaseCrash reporting initialized com.google.android.gms.internal.zzdzk@ce3f80f
04-01 14:18:55.315 30030-30070/? D/FirebaseCrash: Firebase Analytics Listener for Firebase Crash is initialized
04-01 14:18:55.320 30030-30072/? I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:12 and remote module com.google.android.gms.tagmanager:12
04-01 14:18:55.320 30030-30072/? I/DynamiteModule: Selected remote version of com.google.android.gms.tagmanager, version >= 12
04-01 14:18:55.325 30030-30089/? D/OpenGLRenderer: HWUI GL Pipeline
04-01 14:18:55.330 30030-30072/? W/GoogleTagManager: No container asset found in /assets/containers. Checking top level /assets directory for container assets.
04-01 14:18:55.332 30030-30072/? W/GoogleTagManager: Tag Manager's event handler WILL NOT be installed (no container loaded)
04-01 14:18:55.332 30030-30072/? I/GoogleTagManager: Tag Manager initilization took 4ms
04-01 14:18:55.334 30030-30072/? D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-3841173524791218913}]
04-01 14:18:55.352 30030-30072/? V/FA: Connection attempt already in progress
04-01 14:18:55.367 30030-30089/? I/Adreno: QUALCOMM build : 076f837, I801ffd6741
Build Date : 10/24/17
OpenGL ES Shader Compiler Version: EV031.20.00.04
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
04-01 14:18:55.370 30030-30089/? I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
04-01 14:18:55.370 30030-30089/? I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
04-01 14:18:55.391 30030-30089/? I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
04-01 14:18:55.395 30030-30089/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-01 14:18:55.395 30030-30089/? D/OpenGLRenderer: Swap behavior 2
04-01 14:18:55.445 30030-30089/? I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
04-01 14:18:55.606 30030-30072/? D/FA: Connected to remote service
04-01 14:18:55.606 30030-30072/? V/FA: Processing queued up service tasks: 4
04-01 14:18:59.664 30030-30072/com.example.froggy.Demo21C V/FA: Recording user engagement, ms: 4353
04-01 14:18:59.666 30030-30072/com.example.froggy.Demo21C V/FA: Activity paused, time: 1234539067
04-01 14:18:59.669 30030-30072/com.example.froggy.Demo21C D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=4353, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-3841173524791218913}]
04-01 14:18:59.680 30030-30030/com.example.froggy.Demo21C V/FA: onActivityCreated
04-01 14:18:59.745 30030-30424/com.example.froggy.Demo21C I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
04-01 14:18:59.745 30030-30424/com.example.froggy.Demo21C I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
04-01 14:18:59.755 30030-30072/com.example.froggy.Demo21C V/FA: Activity resumed, time: 1234539159
04-01 14:18:59.757 30030-30072/com.example.froggy.Demo21C D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-3841173524791218913, firebase_screen_class(_sc)=ViewActiveRepairTickets, firebase_screen_id(_si)=-3841173524791218912}]
04-01 14:18:59.770 30030-30425/com.example.froggy.Demo21C D/NetworkSecurityConfig: No Network Security Config specified, using platform default
04-01 14:19:00.024 30030-30036/com.example.froggy.Demo21C I/zygote64: Do partial code cache collection, code=27KB, data=29KB
04-01 14:19:00.024 30030-30036/com.example.froggy.Demo21C I/zygote64: After code cache collection, code=27KB, data=29KB
04-01 14:19:00.024 30030-30036/com.example.froggy.Demo21C I/zygote64: Increasing code cache capacity to 128KB
04-01 14:19:00.324 30030-30036/com.example.froggy.Demo21C I/zygote64: Do partial code cache collection, code=36KB, data=53KB
04-01 14:19:00.325 30030-30036/com.example.froggy.Demo21C I/zygote64: After code cache collection, code=36KB, data=53KB
04-01 14:19:00.325 30030-30036/com.example.froggy.Demo21C I/zygote64: Increasing code cache capacity to 256KB
04-01 14:19:00.325 30030-30036/com.example.froggy.Demo21C I/zygote64: Compiler allocated 8MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
04-01 14:19:00.811 30030-30432/com.example.froggy.Demo21C I/FirebaseCrash: Sending crashes
04-01 14:19:01.371 30030-30030/com.example.froggy.Demo21C E/ViewActiveRepairTickets: Unable to create temporary file, /data/user/0/com.example.froggy.Demo21C/cache/dir/activetickets3428182938825802874.csv
04-01 14:19:04.784 30030-30072/com.example.froggy.Demo21C V/FA: Inactivity, disconnecting from the service
答案 0 :(得分:0)
请试试这个:
File file = File.createTempFile("activetickets", "csv", getApplicationContext().getFilesDir());
或
File file = File.createTempFile("activetickets", "csv", null);
因为第三个参数的任何更改都不会保证会产生任何影响:https://developer.android.com/reference/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)
答案 1 :(得分:0)
我已设法使用此
创建临时文件File Outdirectory = this.getCacheDir();
File tempFile = File.createTempFile("prefix", ".csv", Outdirectory);
至于消除,我不完全确定。我,手动,经常删除最旧的文件。
更新: 您可以查看文档以更清楚地了解这一点。这些是其创建的推荐做法
https://developer.android.com/training/data-storage/files.html#WriteCacheFileInternal
答案 2 :(得分:0)
这是因为您尝试使用以下方法创建不正确的文件:
File file = File.createTempFile("dir/activetickets", ".csv", getCacheDir());
当您使用"dir/activetickets"
时,表示您要创建一个"/"
字符的文件。它是文件名的无效字符。所以,它不会起作用。
您需要首先删除"dir/"
或创建文件夹"dir"
。
这将有效:
File file = File.createTempFile("activetickets", ".csv", getCacheDir());