写入内部文件(files-folder)时遇到问题。在这里,我试图将我的日志备份到文件夹中的文件。这是我的代码。
package com.rss.feedster.rss_feedster.utility;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.util.Log;
import com.rss.feedster.rss_feedster.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class LogBackup {
private static LogBackup log = null;
private static FileOutputStream fos = null;
private static Date dateTimeStamp;
private static final String TAG = Constants.TAG+" : "+LogBackup.class.getSimpleName();
private LogBackup(){
dateTimeStamp = new Date();
}
private static String getDateTime(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MMM-yyyy hh:mm:ss", Locale.getDefault());
return dateFormat.format(date);
}
public static LogBackup getLogBackup(FileOutputStream fos){
if(log==null)
log = new LogBackup();
log.fos = fos;
return log;
}
public static void writeLog(String msg) {
if(fos!=null) {
try {
fos.write((getDateTime(dateTimeStamp)+" "+msg+"\n").getBytes());
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Log.e(TAG, "fos = null");
}
}
}
这里fos是使用openFileOutput
方法获得的。
我收到以下错误
java.io.IOException: write failed: EBADF (Bad file number)
at libcore.io.IoBridge.write(IoBridge.java:502)
at java.io.FileOutputStream.write(FileOutputStream.java:186)
at java.io.OutputStream.write(OutputStream.java:82)
at **com.rss.feedster.rss_feedster.utility.LogBackup.writeLog(LogBackup.java:47)**
at com.rss.feedster.rss_feedster.sync.FeedsterSync$2.onDataChange(FeedsterSync.java:107)
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzegf.zza(Unknown Source)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
at com.google.android.gms.internal.zzeig.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.system.ErrnoException: write failed: EBADF (Bad file number)
at libcore.io.Posix.writeBytes(Native Method)
at libcore.io.Posix.write(Posix.java:258)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
at libcore.io.IoBridge.write(IoBridge.java:497)
... 16 more
我还设置了读取和写入权限,如下所示
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
有人可以指出我的代码有什么问题吗?
提前致谢!
以下是使用的代码,我称之为LogBack
代码
FileOutputStream fos;
String logMsg;
try {
fos = getActivity().openFileOutput(Constants.BACKUP_LOG, Context.MODE_APPEND);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
下面的代码行是在catch块内部,我在哪里编写任何日志语句..
logMsg = "Error in FetchRssItemsTask : \n" + e.getStackTrace();
Log.e(TAG, logMsg);
LogBackup.getLogBackup(fos).writeLog(TAG+" "+logMsg);