Android 将文件写入内部存储问题

标签 android file

我在 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/

相关文章:

scala - 使用 sc.textfile 时读取文本文件的是驱动程序还是 worker ?

c++ - SHFileOperation 不会 move 文件夹的所有内容

Android - 将大图像上传到服务器

android - 如何检查android中的互联网连接?

PHP 文件上传错误

JAVA - 访问不同子文件夹中的文件

android - 如何获取 ViewPager 当前 View ,这是在 onPageSelected 中向用户显示的 View

Android Studio - 如何在 webview Lollipop 中上传文件 (Android 5.0)

android - UIAutomator - 如何点击菜单项

python - 在 Python 中创建和写入 pdf 文件