我在 Android 的内部存储中写入文件时遇到一点问题。我正在尝试从一个不是 Activity 的帮助类来完成它。这是我正在使用的代码:
FileOutputStream fos = context.openFileOutput("media", context.MODE_PRIVATE);
fos.write(media.getBytes());
fos.close();
它在第一行抛出一个空指针异常。我尝试使用 this.context.openFileOutput("media",this.context.MODE_PRIVATE);
& this.context.openFileOutput("media",Context.MODE_PRIVATE);
但没有任何帮助。
知道如何解决这个问题吗?
最佳答案
其实我有同样的问题,这里是解决方案:
你可以使用这个:
Activity1.java
onCreate()
helperClass mHelper= new helperClass(CallingActivity.this);
helperClass.java
//declare a context
context refContext;
//constructor
public helperClass(context mContext)
{
refContext=mContext;
}
//and you code
hash= jsonObj.getString("hash");
Log.w("CLIENT AUTH HASH","SHOW CLIENT AUTH HASH : "+hash);
FileOutputStream out = refContext.openFileOutput("autohash",Context.MODE_PRIVATE);
out.write(hash.getBytes());
out.close();
希望这对您有所帮助!
关于Android 将文件写入内部存储问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455634/