【问题标题】:Save internal file in my own internal folder in Android将内部文件保存在我自己的 Android 内部文件夹中
【发布时间】:2011-04-23 20:07:45
【问题描述】:

我尝试在我的文件夹中保存一个 txt 文件,在内部存储中,但我每次都面临同样的问题:

“未找到源”

我以不同的方式编写代码,如下所示,但在所有方面我都有同样的问题。

值得一说,我什至加了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在内部存储不需要的 Manifest.xml 中。

不用说我将文件保存在 /data/data/package/files 路径中没有任何问题,但是当我将文件夹添加到文件的根目录时/data/data/package/files/myforlder/myfile.txt 我面临“找不到源”问题。

你能指出我解决这个问题的正确方向吗?

第二个问题是,用于将文件保存在外部存储中的外部文件夹中。
(例如:sdCard或USB存储)场景不同还是相同?

第一种方式:

OutputStreamWriter out;
try {
    File path=new File(getFilesDir(),"myfolder");
    File mypath=new File(path,"myfile.txt");
    if (!mypath.exists()) {
        out = new OutputStreamWriter(openFileOutput( mypath.getAbsolutePath() , MODE_PRIVATE));
        out.write("test");
        out.close();
    }                           
}

第二种方式:

OutputStreamWriter out;
try {
    ContextWrapper cw = new ContextWrapper(this);
    File path = cw.getDir("myfolder", Context.MODE_PRIVATE);
    if (!path.exists()) {
        path.createNewFile();
        path.mkdir();
    }
    File mypath=new File(path,"myfile.txt");
    if (!mypath.exists()) {
        out = new OutputStreamWriter(openFileOutput( mypath.getAbsolutePath() , MODE_PRIVATE));
        out.write("test");
        out.close();
    }
}

第三种方式:

File path=getFilesDir();
String mypath=path.toString() + "/myfolder";
OutputStreamWriter out;
try {
    File  f = new File(mypath , "/myfile.txt"   );
out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE));
     out.write("test");
     out.close();                   
     }

第四条路:

File path=getFilesDir();

OutputStreamWriter out;
    try {
    File f = new File(path.getPath() + "/myfolder/myfile.txt"   );
    out = new OutputStreamWriter(openFileOutput(f.getPath(), MODE_PRIVATE));
    out.write("test");
    out.close();                    
    }

第五种方式:

File path=getFilesDir();                
OutputStreamWriter out;
try {
    File f = new File(path.getCanonicalPath() + "/myfile.txt");
    out = new OutputStreamWriter(openFileOutput( f.getPath(), MODE_PRIVATE));
    out.write("test");
    out.close();                    
    }

【问题讨论】:

  • 在编写问题时选择代码并按 ctrl+k,这将自动缩进并着色您的代码,因此更具可读性。没有人愿意阅读未缩进和未着色的代码。

标签: android


【解决方案1】:

第一种方式:

您没有创建目录。此外,您将绝对路径传递给openFileOutput(),这是错误的。

第二种方式:

您创建了一个具有所需名称的空文件,这阻止了您创建目录。此外,您将绝对路径传递给openFileOutput(),这是错误的。

第三种方式:

您没有创建目录。此外,您将绝对路径传递给openFileOutput(),这是错误的。

第四条路:

您没有创建目录。此外,您将绝对路径传递给openFileOutput(),这是错误的。

第五种方式:

您没有创建目录。此外,您将绝对路径传递给openFileOutput(),这是错误的。

正确方法:

  1. 为您想要的目录创建一个File(例如,File path=new File(getFilesDir(),"myfolder");
  2. 如果该目录不存在,请在该File 上调用mkdirs() 以创建目录
  3. 为输出文件创建一个File(例如File mypath=new File(path,"myfile.txt");
  4. 使用standard Java I/O 写入File(例如,使用new BufferedWriter(new FileWriter(mypath))

【讨论】:

  • 该文件和文件夹是在内存还是sdcard中创建的?
  • @Shoshi:getFilesDir()代表内部存储。
  • @Mr.Hyde:getFilesDir()私人的。除了您的应用程序之外,没有其他应用程序可以访问它。例外情况是在根设备上,其中 nothing 是私有的。
  • @Mr.Hyde:正确。您不能将 APK 设为私有并能够安装它,因为您不是安装程序应用程序,并且安装程序应用程序需要读取 APK 的权限才能安装它。现在,正确的解决方案是使用 FileProvider 之类的东西,并为安装程序应用程序提供具有临时权限的 content Uri。不幸的是,这仅适用于 Android 7.0+,因为早期版本的 Android 不支持应用程序安装的 content Uri 值。在 Android 7.0 之前,您唯一的选择是在安装过程中将 APK 放在外部存储中。
  • @Mr.Hyde:AFAIK,pm install 仍然要求安装应用程序具有对 APK 的读取权限。我从来没有尝试过pm installcontent Uri——我只用过文件系统路径。如果“使用 AOSP”表示您正在构建自定义 ROM,您可以尝试在安装程序中启用 content 方案支持。这将需要将content 添加到<intent-filter>,并且可能需要更改安装程序应用程序在APK 中的读取方式。您可以在您的副本和 7.0 源之间运行差异,以查看启用此功能的更改。
【解决方案2】:

保存:

public boolean saveFile(Context context, String mytext){
    Log.i("TESTE", "SAVE");
    try {
        FileOutputStream fos = context.openFileOutput("file_name"+".txt",Context.MODE_PRIVATE);
        Writer out = new OutputStreamWriter(fos);
        out.write(mytext);
        out.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

加载:

public String load(Context context){
    Log.i("TESTE", "FILE");
    try {
        FileInputStream fis = context.openFileInput("file_name"+".txt");
        BufferedReader r = new BufferedReader(new InputStreamReader(fis));
        String line= r.readLine();
        r.close();
        return line;
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TESTE", "FILE - false");
        return null;
    }
}

【讨论】:

    【解决方案3】:

    Mintir4的回答很好,我也会做以下加载文件。

    FileInputStream fis = myContext.openFileInput(fn);
    BufferedReader r = new BufferedReader(new InputStreamReader(fis));
    String s = "";
    
    while ((s = r.readLine()) != null) {
        txt += s;
    }
    
    r.close();
    

    【讨论】:

    • 请编辑您的答案并格式化代码以使其可读
    猜你喜欢
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 2021-09-18
    • 2023-03-10
    • 2016-11-01
    • 1970-01-01
    相关资源
    最近更新 更多