从SD卡创建文件/数据库

时间:2018-10-17 09:09:30

标签: java android database file import

我是Android Studio的初学者

我还可以从手机存储创建文件,但是我需要如何从SD卡创建文件。我正在使用 虚拟设备或i9000S

实际上我在使用:

Android Jellybean

API级别:18

Android版本:4.3

如果我使用此 File myFile = new File("/sdcard/sample.txt"); ,它将起作用。

当我使用此 File myFile = new File("/sdcard1/sample.txt"); 时,它不起作用。它给我一个类似 Error: open failed: ENOENT (No such file or directory).

的错误

MainActivity.java:

final String NEW_FOLDER_NAME = "TestFolder";
testPath(new File(Environment.getExternalStorageDirectory(), NEW_FOLDER_NAME));
testPath(new File("/storage/emulated/0/", NEW_FOLDER_NAME));
testPath(new File("/storage/emulated/1/", NEW_FOLDER_NAME));
testPath(new File("/storage/sdcard0/Download/", NEW_FOLDER_NAME));
testPath(new File("/storage/sdcard1", NEW_FOLDER_NAME));
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            String E1 = System.getenv("EXTERNAL_STORAGE");
            File F1 = new File(E1, NEW_FOLDER_NAME);
            String E2 = System.getenv("SECONDARY_STORAGE");
            File F2 = new File(E2, NEW_FOLDER_NAME);
            testPath(new File("/storage/sdcard1", NEW_FOLDER_NAME));
            testPath(F1);
            testPath(F2);

            File myFile = new File("/sdcard1/sample.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =
                    new OutputStreamWriter(fOut);
            myOutWriter.append(e1.getText());
            myOutWriter.close();
            fOut.close();
            Toast.makeText(Main1Activity.this, "Save to" + getFilesDir() + ">" + NEW_FOLDER_NAME, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }
});

private void testPath(File path) {
    String TAG = "Debug.MainActivity.java";
    String FOLDER_CREATION_SUCCESS = " mkdir() success: ";
    boolean success;
    if (path.exists()) {
        // already created
        success = true;
    } else {
        success = path.mkdir();
    }
    Log.d(TAG, path.getAbsolutePath() + FOLDER_CREATION_SUCCESS + success);
    path.delete();
}

修改: 我已经从清单文件中添加了

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

2 个答案:

答案 0 :(得分:0)

摘自官方文件- 该代码片段将在内部存储器上创建文件。不需要WRITE_EXTERNAL_STORAGE权限。

// Create new file and write 
File file = new File(context.getFilesDir(), filename);

String filename = "yourFileName.txt";
String fileContents = "Insert your data here.";
FileOutputStream outputStream;

try {
    outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
    outputStream.write(fileContents.getBytes());
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}


// Opening a file 
File directory = context.getFilesDir();
File file = new File(directory, filename);
// Here you can re-write or edit your file

请看一下- Read/Write internal storage

答案 1 :(得分:0)

要获取sdcard根路径,您应该使用

Environment.getExternalStoragexxx