如何在SD卡中存储文件

时间:2016-07-23 09:47:45

标签: android

我使用以下代码在sdcard中创建文件,但文件是

以下代码在内部存储中创建文件

     private String filename = "SampleFile.txt";
     private String filepath = "MyFileStorage";

        if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
                    saveButton.setEnabled(false);
                }
                else {
                    myExternalFile = new File(getExternalFilesDir(filepath), filename);
                }

      private static boolean isExternalStorageReadOnly() {
            String extStorageState = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
                return true;
            }
            return false;
        }

        private static boolean isExternalStorageAvailable() {
            String extStorageState = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
                return true;
            }
            return false;
        }

   FileOutputStream fos = new FileOutputStream(myExternalFile);

fos.write(inputText.getText().toString().getBytes());
fos.close();

在清单中添加权限以使其正常工作..

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

0 个答案:

没有答案