如何在Android手机上创建用户可访问的文件?

如何在Android手机上创建用户可访问的文件?,android,file,external,Android,File,External,我正在尝试将应用程序中的数据保存到一个文件中,然后将该文件保存到我的PC上。我已经阅读了许多关于在外部存储上创建文件的教程。根据这些指南,我创建了下面的代码。手机没有SD卡,外部存储器位于内部存储器中。所以我的问题是:文件在使用“writeFunc”后仍然存在,但它是空的。不会引发任何异常。 我用所有现有的标志尝试了这一部分,可惜没有结果: FileOutputStream fOut=openFileOutput(fileStruct.getName(),模式为世界可读) setReadable

我正在尝试将应用程序中的数据保存到一个文件中,然后将该文件保存到我的PC上。我已经阅读了许多关于在外部存储上创建文件的教程。根据这些指南,我创建了下面的代码。手机没有SD卡,外部存储器位于内部存储器中。所以我的问题是:文件在使用“writeFunc”后仍然存在,但它是空的。不会引发任何异常。 我用所有现有的标志尝试了这一部分,可惜没有结果:

FileOutputStream fOut=openFileOutput(fileStruct.getName(),模式为世界可读)

setReadable和setWritable函数返回“false”,但没有异常

否则我该怎么办

package com.track.fos;
//imports are okay

public class MainActivity extends AppCompatActivity {

public TextView textBox;
private File fileStruct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textBox = (TextView) findViewById(R.id.textView);
    textBox.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());

    fileStruct = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "lufiTestFile.txt" );
}

public void writeFunc(View view) {

    textBox.append("\nFilestruct to string: " + fileStruct.toString());
    Log.d("External storage state", Environment.getExternalStorageState(fileStruct));

    try {
        boolean stateInfo = fileStruct.exists();
        textBox.append("\nexist: " + stateInfo);

        if( !stateInfo ){
            //stateInfo = fileStruct.mkdirs();
            //textBox.append("\nmake dirs: " + stateInfo);
            stateInfo = fileStruct.createNewFile();
            textBox.append("\ncreateNewFile: " + stateInfo);
        }

        stateInfo = fileStruct.setReadable( true , false);
        textBox.append("\nsetReadable: " + stateInfo);
        stateInfo = fileStruct.setWritable( true, false );
        textBox.append("\nsetWritable: " + stateInfo);

        FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
        fOut.write("Test text hopefuly inside the file.".getBytes());
        fOut.flush();
        fOut.close();
        textBox.append("\nFile written");
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("File write throws",e.getCause().toString());
        textBox.append("\nFile write failed");
    }
}

public void readFunc(View view) {

    try {
        FileInputStream fin = openFileInput( fileStruct.getName() );
        int c;
        String temp = "";
        while ((c = fin.read()) != -1) {
            temp = temp + Character.toString((char) c);
        }
        textBox.append("\n" + temp);
    } catch (Exception e) {
        e.printStackTrace();
        textBox.append("\nFile read failed");
    }
}
}
这将不会为上的文件打开
FileOutputStream
<代码>openFileOutput()写入

替换为:

FileOutputStream fOut = new FileOutputStream(fileStruct);
你也需要这样做

FileOutputStream fOut = new FileOutputStream(fileStruct);