0

この関数はファイルを作成しますが、ファイルが作成された場所がわかりません。誰かが外部ストレージから特定のディレクトリにファイルを作成する解決策を持っている場合は大歓迎です:)どうもありがとう

private void writeFileToInternalStorage() {
    String eol = System.getProperty("line.separator");
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile", MODE_WORLD_WRITEABLE)));
      writer.write("This is a test1." + eol);
      writer.write("This is a test2." + eol);
    } catch (Exception e) {
            e.printStackTrace();
    } finally {
      if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
      }
    }
}
4

3 に答える 3

2

openFileOutput常にローカルプロジェクトディレクトリにファイルを作成します。つまり、によって返されるものと同じです。 getFilesDir()

あなたのファイルはで作成されます /data/data/your.package.name/

外部ストレージに書き込むには、次のようなことを行う必要があります。

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/MyDir");
dir.mkdirs();
File file = new File(dir, "filename");

FileOutputStream f = new FileOutputStream(file);
...

最後に含めることを忘れないでください

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

マニフェストで。

于 2012-06-26T14:22:12.083 に答える
0

内部フォルダーに作成されます/data/data/com.package.name/。ファイル ブラウザーを使用してそのフォルダーにアクセスすることはできません。

ファイルに簡単にアクセスしたい場合は、SD カードに作成してみてください。

/*...*/
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = baseDir + "/"+ "myFile.txt";
FileOutputStream writer = null;
try {
    writer = new FileOutputStream(fileName);
    writer.write("This is a test1." + eol);
    /*...*/
于 2012-06-26T14:25:06.123 に答える
0

クエリ用

  • ファイルが作成される場所

関数名が示すように内部ストレージに作成され、/data/data/yourApp_package_as_in_manifest/ のようになります (DDMS で確認できます)

クエリ用

  • 誰かが外部ストレージから特定のディレクトリにファイルを作成するソリューションを持っている場合は大歓迎です

リンクごとに Androidの外部ストレージにファイルを書き込む .........

** Method to check whether external media available and writable. This is adapted from
       http://developer.android.com/guide/topics/data/data-storage.html#filesExternal */

     private void checkExternalMedia(){
          boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // Can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // Can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Can't read or write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }   
        tv.append("\n\nExternal Media: readable="
                +mExternalStorageAvailable+" writable="+mExternalStorageWriteable);
    }

    /** Method to write ascii text characters to file on SD card. Note that you must add a 
       WRITE_EXTERNAL_STORAGE permission to the manifest file or this method will throw
       a FileNotFound Exception because you won't have write permission. */

    private void writeToSDFile(){

        // Find the root of the external storage.
        // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

        File root = android.os.Environment.getExternalStorageDirectory(); 
        tv.append("\nExternal file system root: "+root);

        // See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

        File dir = new File (root.getAbsolutePath() + "/download");
        dir.mkdirs();
        File file = new File(dir, "myData.txt");

        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println("Hi , How are you");
            pw.println("Hello");
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                    " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        }   
        tv.append("\n\nFile written to "+file);
    }

また、マニフェストに WRITE_EXTERNAL_STORAGE 権限を追加します

于 2012-06-26T14:25:13.987 に答える