简体   繁体   English

Android Emulator中的问题编写文件问题

[英]Problem Writing Files problem in Android Emulator

I am trying to write some simple data to External storage using the following code. 我正在尝试使用以下代码将一些简单数据写入外部存储。 I am missing something here but not sure what. 我在这里遗漏了一些东西,但不确定是什么。 Thanks 谢谢

RobD RobD

public class TimeCard extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button button = (Button) findViewById(R.id.Button01);
        final EditText edittext = (EditText) findViewById(R.id.EditText01);
        final EditText edittext1 = (EditText)findViewById(R.id.EditText02);
        final EditText edittext2 = (EditText)findViewById(R.id.EditText03);
        final EditText edittext3 = (EditText)findViewById(R.id.EditText04);
        final String inputString = edittext + "/n" + edittext1 + "/n"+  edittext2 + "/n" + edittext3;
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {               
              //Do it
              CreateFile(inputString);
              //Let them know it
              Toast.makeText(TimeCard.this, "You are Clocked In", Toast.LENGTH_LONG).show();
            }


        });
    }
    //Write to SD Card
 public void CreateFile(String InputString){        

     File SDCard = Environment.getExternalStorageDirectory();  
     String FILENAME = SDCard + "/time_card.txt";

        FileOutputStream fos = null;
        try {
            fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        try {
            fos.write(InputString.getBytes());
        } catch (IOException e) {

            e.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
        }
   }

I tried running your code, and the error is caused by the way you use the openFileOutput(String name, int mode) method. 我尝试运行您的代码,错误是由您使用openFileOutput(String name, int mode)方法引起的。 Looking inside LogCat, I can see the following exception: 在LogCat里面,我可以看到以下异常:

java.lang.IllegalArgumentException: File /mnt/sdcard/time_card.txt contains a path separator

This pointed me in the direction of the answer to this question , which will probably solve your problem as well: 这使我指出了这个问题的答案方向,这也可能解决你的问题:

Context.openFileOutput is meant to be used for creating files private to your application. Context.openFileOutput用于创建应用程序专用的文件。 They go in your app's private data directory. 它们会进入您应用的私人数据目录。 You supply a name, not a path 您提供名称,而不是路径

The documentation for openFileOutput also indicates this about the name parameter of the function: openFileOutput文档也指出了这个函数的name参数:

The name of the file to open; 要打开的文件的名称; can not contain path separators. 不能包含路径分隔符。

For future references, when you experience problems like this, it is absolutely vital that you learn how to use the tools available to you, such as LogCat. 对于将来的参考,当您遇到这样的问题时,了解如何使用可用的工具(如LogCat)绝对至关重要。 Without them, you'll have a hard time figuring out what is wrong. 没有他们,你将很难搞清楚什么是错的。 So I recommend reading up a bit on how to do this. 所以我建议你阅读一下如何做到这一点。

It is hard to tell when you don't show what kind of error message you get. 当你没有显示你得到什么样的错误信息时,很难说清楚。 But my first guess is that you have forgotten to include the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file. 但我的第一个猜测是您忘记在AndroidManifest.xml文件中包含WRITE_EXTERNAL_STORAGE权限。

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

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