如何编写其他应用程序可读的文件?

发布于 2024-12-28 13:41:25 字数 1084 浏览 2 评论 0原文

我的应用程序创建一个文本文件。我希望用户能够轻松访问这些文件。我将这个文本文件写入 SD 卡上。我想允许用户使用其他程序编辑该文件。
我使用文件资源管理器看到这些文件,但使用文件管理器看不到。

String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        try{
    String pathPrefix = extPath + "/Android/data/" +      getApplication().getPackageName() + "/cache/";
    //File extPathFile = getExternalFilesDir(pathPrefix+FileName);
    Log.d("TAG", "pathPrefix="+pathPrefix);
    Log.d("TAG", "FileName="+FileName);
    File file = new File(pathPrefix, FileName+".txt");
    if(file.exists()){
    Log.d("TAG", "File("+pathPrefix+FileName+")exist"); 
    };
   OutputStreamWriter outStream = 
            new OutputStreamWriter(new FileOutputStream(file));

日志猫:

01-22 14:26:15.834: D/TAG(14290): pathPrefix=/mnt/sdcard/Android/data/mast.avalons/cache/
01-22 14:26:15.834: D/TAG(14290): FileName=S000004_21-10-2011
01-22 14:26:16.044: D/TAG(14290): java.io.FileNotFoundException: /mnt/sdcard/Android/data/mast.avalons/cache/S000004_21-10-2011.txt (No such file or directory)

谢谢!

My application creates a text file. I want that users to have easy access to these files. I write this text file them on the sdcard. I want to allow users to edit the file with other programs.
I see these files using the File Explorer, but I can not see using file manager.

String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        try{
    String pathPrefix = extPath + "/Android/data/" +      getApplication().getPackageName() + "/cache/";
    //File extPathFile = getExternalFilesDir(pathPrefix+FileName);
    Log.d("TAG", "pathPrefix="+pathPrefix);
    Log.d("TAG", "FileName="+FileName);
    File file = new File(pathPrefix, FileName+".txt");
    if(file.exists()){
    Log.d("TAG", "File("+pathPrefix+FileName+")exist"); 
    };
   OutputStreamWriter outStream = 
            new OutputStreamWriter(new FileOutputStream(file));

Logcat:

01-22 14:26:15.834: D/TAG(14290): pathPrefix=/mnt/sdcard/Android/data/mast.avalons/cache/
01-22 14:26:15.834: D/TAG(14290): FileName=S000004_21-10-2011
01-22 14:26:16.044: D/TAG(14290): java.io.FileNotFoundException: /mnt/sdcard/Android/data/mast.avalons/cache/S000004_21-10-2011.txt (No such file or directory)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2025-01-04 13:41:25

我认为你应该创建中间目录:

new File(pathPrefix).mkdirs();

I think you should create intermediate directories :

new File(pathPrefix).mkdirs();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文