简体   繁体   English

Android内部存储; 创建文件

[英]Internal Storage for Android; creating files

Im trying to just create basic files internally for another app. 我试图只是在内部为另一个应用程序创建基本文件。 So i wrote a basic app to work out the kinks then was going to add it to the other app. 所以我写了一个基本的应用程序来解决问题,然后将其添加到另一个应用程序中。 Here is the log from the cat 这是猫的日志

10-09 17:56:14.579: D/dalvikvm(11092): Not late-enabling CheckJNI (already on)
10-09 17:56:15.599: E/Trace(11092): error opening trace file: No such file or directory (2)
10-09 17:56:16.549: D/gralloc_goldfish(11092): Emulator without GPU emulation detected.
10-09 17:57:20.158: D/dalvikvm(11092): Debugger has detached; object registry had 1 entries
10-09 17:57:30.078: E/Trace(11662): error opening trace file: No such file or directory (2)
10-09 17:57:30.759: D/gralloc_goldfish(11662): Emulator without GPU emulation detected.

The file is created. 文件已创建。 But when im implemented the code to create a file off the onClick it did not. 但是,当im实现代码以通过onClick创建文件时,它没有。 Or when i put the file creating in a class other than the main class, it did not create the file. 或者,当我将创建的文件放在主类以外的其他类中时,它没有创建文件。

here is my basic code: `package com.newapp; 这是我的基本代码:`package com.newapp;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    newFile();

}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public String timeStamp(){
        Date myDate = new Date();
        return (DateFormat.getDateInstance().format(myDate) + " " +                         DateFormat.getTimeInstance().format(myDate));
    }

    public void newFile (){
         String FILENAME = timeStamp();
         String string = "hello world!";

         FileOutputStream fos = null;
            try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
        }
         try {
                fos.write(string.getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
        e.printStackTrace();
        }
         try {
                    fos.close();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}`

I got rid of the button and other classes for now. 我现在摆脱了按钮和其他类。 This is all the code other than the normal hello worlds screen they give you and an unmodified manifest. 这是所有代码,除了它们为您提供的普通hello world屏幕以及未修改的清单之外。

EDIT The code above works i and a file is created in /data/data/com.newapp/files Just ran it again to make sure a file is created and got this from the cat. 编辑上面的代码可以正常工作,并且在/data/data/com.newapp/files中创建了一个文件。只需再次运行它,以确保创建了文件并从猫那里得到了它。

10-09 17:57:29.482: D/dalvikvm(11662): Not late-enabling CheckJNI (already on) 10-09 17:57:30.078: E/Trace(11662): error opening trace file: No such file or directory (2) 10-09 17:57:30.759: D/gralloc_goldfish(11662): Emulator without GPU emulation detected. 10-09 17:57:29.482:D / dalvikvm(11662):不晚启用CheckJNI(已启用)10-09 17:57:30.078:E / Trace(11662):打开跟踪文件时出错:没有这样的文件或目录(2)10-09 17:57:30.759:D / gralloc_goldfish(11662):未检测到GPU仿真的仿真器。 10-09 18:53:35.238: D/dalvikvm(11662): Debugger has detached; 10-09 18:53:35.238:D / dalvikvm(11662):调试器已分离; object registry had 1 entries 10-09 18:53:53.389: E/Trace(14975): error opening trace file: No such file or directory (2) 10-09 18:53:54.658: I/Choreographer(14975): Skipped 36 frames! 对象注册表有1个条目10-09 18:53:53.389:E / Trace(14975):打开跟踪文件时出错:没有这样的文件或目录(2)10-09 18:53:54.658:I / Choreographer(14975):跳过36帧! The application may be doing too much work on its main thread. 该应用程序可能在其主线程上做太多工作。 10-09 18:53:54.668: D/gralloc_goldfish(14975): Emulator without GPU emulation detected. 10-09 18:53:54.668:D / gralloc_goldfish(14975):未检测到GPU仿真的仿真器。

I am going to move it to its own class now and try it. 我现在将其移至其自己的班级并尝试。

Here is the file creation in its own class: package com.newapp; 这是在其自己的类中的文件创建:包com.newapp;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;    
import java.util.Date;

import android.app.Activity;
import android.content.Context;

public class NewFile extends Activity {

public String timeStamp(){
    Date myDate = new Date();
    return (DateFormat.getDateInstance().format(myDate) + " " +             DateFormat.getTimeInstance().format(myDate));
}

public NewFile (){
     String FILENAME = timeStamp();
     String string = "hello world!";

     FileOutputStream fos = null;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
 }

And here is the Main activity: 这是主要活动:

package com.newapp;



import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NewFile firstone = new NewFile();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}   
}

Finally I tried it a few different ways with having the NewFile(); 最后,我尝试了几种不同的方式来使用NewFile(); as a constructor as just a regular method and calling firstone.newFile(); 作为构造函数,就像常规方法一样,并调用firstone.newFile(); Having a lot of trouble it will not even run on the virtual device or tablet right now. 遇到很多麻烦,它现在甚至无法在虚拟设备或平板电脑上运行。

I figured out the problem with the help of a friend. 我在朋友的帮助下找到了问题所在。 You cannot "extend activity" in the new class. 您不能在新课程中“扩展活动”。 You need to use context to reference the class. 您需要使用上下文来引用该类。 The final code for the NewFile class is: NewFile类的最终代码是:

package com.newapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import android.content.Context;

public class NewFile{

public String timeStamp(){
    Date myDate = new Date();
    return (DateFormat.getDateInstance().format(myDate) + " " + DateFormat.getTimeInstance().format(myDate));
}

public void createFile(Context c) throws IOException{
     String FILENAME = timeStamp();
     String string = "hello world!";

     FileOutputStream fos = c.openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();


}
}

and in the main class you just needed to call: 在主类中,您只需要调用:

NewFile firstfile = new NewFile();
firstfile.createFile(getBaseContext());

In the onCreate method. 在onCreate方法中。

Thanks 谢谢

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

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