如何在 Android phone 上创建用户可访问的文件?
How to create user-accessible file on Android phone?
我正在尝试将数据从我的应用程序保存到一个文件,而不是将这个文件保存到我的电脑上。我读过很多关于在外部存储上创建文件的教程。按照本指南,我创建了以下代码。 phone没有SD卡,外存在内存上。所以我的问题是:使用 "writeFunc" 后文件存在,但它是空的。不会抛出任何异常。
我用所有现有的标志尝试了这部分,遗憾的是没有结果:
FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
setReadable 和 setWritable 函数 return 和 "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 fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
不会打开 external storage. openFileOutput()
writes to internal storage 上的文件的 FileOutputStream
。
将其替换为:
FileOutputStream fOut = new FileOutputStream(fileStruct);
您还需要。
我正在尝试将数据从我的应用程序保存到一个文件,而不是将这个文件保存到我的电脑上。我读过很多关于在外部存储上创建文件的教程。按照本指南,我创建了以下代码。 phone没有SD卡,外存在内存上。所以我的问题是:使用 "writeFunc" 后文件存在,但它是空的。不会抛出任何异常。 我用所有现有的标志尝试了这部分,遗憾的是没有结果:
FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
setReadable 和 setWritable 函数 return 和 "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 fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
不会打开 external storage. openFileOutput()
writes to internal storage 上的文件的 FileOutputStream
。
将其替换为:
FileOutputStream fOut = new FileOutputStream(fileStruct);
您还需要