【问题标题】:How to create user-accessible file on Android phone?如何在 Android 手机上创建用户可访问的文件?
【发布时间】:2017-06-08 21:02:44
【问题描述】:

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

FileOutputStream fOut = openFileOutput(fileStruct.getName(), MODE_WORLD_READABLE);

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");
    }
}
}

【问题讨论】:

    标签: android file external


    【解决方案1】:
    FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
    

    这不会为external storage 上的文件打开FileOutputStreamopenFileOutput() 写信给internal storage

    替换为:

    FileOutputStream fOut = new FileOutputStream(fileStruct);
    

    您还需要ensure that the file gets indexed, so other apps and desktop OSes can see it

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多