简体   繁体   English

Android AsyncTask使用openFileOutputStream

[英]Android AsyncTask use openFileOutputStream

i have a error in my AsyncTask. 我的AsyncTask中有错误。 I tried to save Data with FileOutputStream to a File, cause i need this Data permanent. 我试图用FileOutputStream将数据保存到文件,因为我需要这个数据永久。

So iam reading this tutorial: Tutorial 所以我读这个教程: 教程

But if iam adding the Code to my AsyncTask i get this error: 但是如果我将代码添加到我的AsyncTask中,我会收到此错误:

"The method openFileOutputStream(String, int) is undefined for the type MainActivity.DownloadSpielplan" “对于类型MainActivity.DownloadSpielplan,未定义方法openFileOutputStream(String,int)”

DownloadSpielplan is my AsyncTask DownloadSpielplan是我的AsyncTask

private class DownloadSpielplan extends AsyncTask <Void, Void, String>
    {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }
        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub


            //dont wonder reverseString is created and filled i delete this part from the code for more readability


            FILENAME = "SpielTag";
            JOUR = reverseString;

            FileOutputStream fos = openFileOutputStream(FILENAME, Context.MODE_PRIVATE);
            fos.write(JOUR.getBytes());
            fos.close();


            return reverseString;
        }

        @Override
        protected void onPostExecute(String reverseString) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Download abgeschlossen!", Toast.LENGTH_LONG).show();
            super.onPostExecute(reverseString);
        }
    }   

I guess the Problem is, iam calling openFileOutputStream from the AsyncTask, but i cant find a solution how to fix it. 我想问题是,我从AsyncTask调用openFileOutputStream,但我找不到解决方法如何解决它。 (cause iam realy new in Android) (导致我在Android中真的很新)

Method name is openFileOutput, not openFileOutputStream 方法名称是openFileOutput,而不是openFileOutputStream

Call 呼叫

MainActivity.this.openFileOutput(FILENAME, Context.MODE_PRIVATE)

instead of 代替

openFileOutputStream(FILENAME, Context.MODE_PRIVATE)

in your doInBackground method. 在你的doInBackground方法中。

Because you are trying to access the method openFileOutputStream from a class which is not an Activity . 因为您尝试从不是Activity的类访问方法openFileOutputStream Instead, write your code as- 相反,写下您的代码 -

FileOutputStream fos = MainActivity.this.openFileOutputStream(FILENAME, Context.MODE_PRIVATE);

or an other option would be pass the context from the activity calling the DownloadSpielplan in its constructor and use the context to open file output- 或者另一个选项是从在其构造函数中调用DownloadSpielplan的活动传递上下文并使用上下文打开文件输出 -

FileOutputStream fos = context.openFileOutputStream(FILENAME, Context.MODE_PRIVATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM