android - Android 的内部存储;创建文件

标签 android file storage internal

我正在尝试为另一个应用程序在内部创建基本文件。所以我写了一个基本的应用程序来解决问题,然后将它添加到另一个应用程序中。这是猫的日志

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.

文件已创建。但是,当我实现代码以通过 onClick 创建文件时,它没有。或者当我将文件创建在主类以外的类中时,它没有创建文件。

这是我的基本代码: `包 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();
        }
    }
}`

我暂时摆脱了按钮和其他类。这是除了他们给你的正常 hello worlds 屏幕和未修改的 list 之外的所有代码。

编辑 上面的代码有效,并且在/data/data/com.newapp/files 中创建了一个文件 再次运行它以确保创建了一个文件并从猫那里得到了它。

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): 调试器已分离;对象注册表有 1 个条目 10-09 18:53:53.389: E/Trace(14975): 打开跟踪文件时出错:没有这样的文件或目录 (2) 10-09 18:53:54.658:我/编舞(14975):跳过了 36 帧!应用程序可能在其主线程上做了太多工作。 10-09 18:53:54.668: D/gralloc_goldfish(14975): 没有检测到 GPU 模拟的模拟器。

我现在要把它移到自己的类中试试。

这是在它自己的类中创建的文件: 包 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();
    }
    }
 }

这是主要 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;
}   
}

最后,我尝试了几种不同的方法来使用 NewFile();作为构造函数作为常规方法并调用 firstone.newFile(); 遇到很多麻烦,它现在甚至无法在虚拟设备或平板电脑上运行。

最佳答案

在 friend 的帮助下我解决了这个问题。 您不能在新类(class)中“扩展 Activity ”。 您需要使用上下文来引用该类。 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();


}
}

在主类中你只需要调用:

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

在onCreate方法中。

谢谢

关于android - Android 的内部存储;创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13185543/

相关文章:

android - 从 SQLite 数据库中获取当前日期到上个月日期之间的数据

java - 谷歌地图实现java.lang.RuntimeException : Unable to start activity

android - 三星我的文件资源管理器 : Pick file Intent

ios - 使用 CoreData 和 Swift 2.0 保存问题

共享主机中的 Laravel 存储链接

Android 测试存储模拟

javascript - 将文件从 android_asset 文件夹复制到 SD 卡

android -/proc/net/tcp和/proc/pid/net/tcp有什么区别

C - 使用结构成员值的段错误

file - 我想用 sed 命令编辑特定行(多行)