创建文件夹并将文件写入外部存储 [英] Creating folders and writing files to external storage

查看:190
本文介绍了创建文件夹并将文件写入外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建目录,然后将数据写入外部SD卡上的新文件中。我以前已经成功地将文件写入内部存储,但是与此同时,我似乎有2个问题(除了我自己,因为这只是我的第二个应用程序!)。

I am trying to create a directory and then write data into a new file on an external SD card. I have previously written files successfully to internal storage, but with this I seem to have 2 problems (well, apart from myself, as this is only my second app!).

已经在stackoverflow上进行了详尽的搜索以获取建议和示例代码,我构建了以下方法:

Having searched at length on stackoverflow for advice and sample code, I have built the following method:

void filewriter(String fnam,String mdata){
       if (useSD) {
           //test to see if a directory called AMJ exists on external storage and create one if it does not
           try {
               File folder = new File(Environment.getExternalStorageDirectory() + "/AMJ");
               if (folder.exists() && folder.isDirectory()) {

                   Toast.makeText(getApplicationContext(), "folder " + folder + " exists", Toast.LENGTH_LONG).show(); //##1
               } else {
                   Toast.makeText(getApplicationContext(), "folder " + folder + " does not exist", Toast.LENGTH_LONG).show(); //##2
                   folder.mkdirs();
               }
           } catch (Exception e) {
               Toast.makeText(getApplicationContext(), "Unable to find or create a directory for route data on SD card", Toast.LENGTH_LONG).show(); //##3
           }
           //create a file with file name fnam and write String mdata into the AMJ directory
           try {
               String namFile = Environment.getExternalStorageDirectory() + "/AMJ/" + fnam;
               File datfile = new File(namFile);
               Toast.makeText(getApplicationContext(), "File is:  " + datfile, Toast.LENGTH_LONG).show(); //##4
               datfile.createNewFile();
               FileOutputStream fOut = new FileOutputStream(datfile);
               OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
               myOutWriter.append(mdata);
               myOutWriter.close();
               fOut.close();
               Toast.makeText(getApplicationContext(), "Finished writing to SD", Toast.LENGTH_LONG).show(); //##5
           } catch (Exception e) {
               Toast.makeText(getApplicationContext(), "Write failure", Toast.LENGTH_SHORT).show(); //##6
           }
       }
   }

fnam和mdata是简单的文本,例如 myfile和这是我的测试数据。

fnam and mdata are simple text such as "myfile" and "This is my test data".

第一个问题是我第一次运行此命令时,会收到吐司消息## 2(文件夹不存在)与我预期的一样。但是,我希望创建一个目录AMJ,以便下次运行代码时,我会收到消息## 1表示该目录现在存在,但是那没有发生-它仍然表示该目录不存在。

The first problem is that the first time I run this I get the toast message ##2 (folder does not exist) as I expected. However, I expected a directory AMJ to be created so that the next time I run the code I would get message ##1 to say that the directory now exists, but that doesn't happen - it still says that the directory does NOT exist.

第二个问题(这当然可能是第一个问题的结果)是文件创建/写入不起作用,并且我收到了 catch消息每次## 6。

The second problem (which may of course just be a consequence of the first) is that the file creation/write does not work and I get the 'catch' message ##6 every time.

我的清单如下:

package="com.example.chris.filewritetests">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我正在Android Studio模拟器上运行它。

I am running this on an Android Studio emulator.

有人可以告诉我这是什么地方吗?

Can anyone tell me what is wrong here, please?

推荐答案

使用Log.e()的建议有所帮助为我指明正确的方向-谢谢。我认为我的问题是我没有意识到内置内存和SD卡不一定与内部和外部存储相同,并且设备有时可以将SD卡上的内存用作内部。

The advice to use Log.e() helped to point me in the right direction - thanks. I think my problem was that I had not realised that built-in memory and SD card were not necessarily the same as internal and external storage, and that a device can sometimes use memory on the SD card as "internal".

我发现我在真实设备上运行代码时发生了什么,其中一个带有SD卡,一个没有SD卡,而不是使用仿真器。这样一来,使用设备的FileCommander即可轻松查看文件夹和文件的创建位置。我建议所有遇到这种问题的人。

I found out what was going on when I ran the code on real devices, one with an SD card and one without one, instead of using the emulator. That made it easy to use the devices' FileCommander to see exactly where folders and files were being created. I would recommend that to anyone struggling with this kind of problem.

这篇关于创建文件夹并将文件写入外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆