Creating folders and writing files to external storage

孤街浪徒 提交于 2019-12-12 02:44:32

问题


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!).

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 and mdata are simple text such as "myfile" and "This is my test data".

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.

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.

My manifest looks like this:

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" />

I am running this on an Android Studio emulator.

Can anyone tell me what is wrong here, please?


回答1:


the directory is not created automatically. You need to create those directories call mkdir() on the file object datfile.mkdirs() change it ti

 try {
           String namFile = Environment.getExternalStorageDirectory() + "/AMJ/" + fnam;
           File datfile = new File(namFile);
           datfile.mkdirs();
           Toast.makeText(getApplicationContext(), "File is:  " + datfile, Toast.LENGTH_LONG).show(); //##4
           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
       }



回答2:


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".

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.



来源:https://stackoverflow.com/questions/44636323/creating-folders-and-writing-files-to-external-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!