没有错误,输入流和输出流中有数据。 不知道为什么执行后找不到这个文件。 似乎没有文件创建时没有错误。 我添加了所有权限。
使用权限 android:name="android.permission.READ_EXTERNAL_STORAGE"
使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE"
使用权限 android:name="android.permission.READ_INTERNAL_STORAGE"
使用权限 android:name="android.permission.WRITE_INTERNAL_STORAGE"
使用权限 android:name="android.permission.WRITE_USER_DICTIONARY"
Bundle bundle = data.getExtras();
ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
// the recording uri is in getData:
Uri audioUri = data.getData();
ContentResolver contentResolver = getContentResolver();
try {
InputStream is = contentResolver.openInputStream(audioUri);
String filename = "testVoice01";
final byte[] buffer = new byte[1024];
int read;
try {
OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);
while ((read = is.read(buffer)) != -1)
{
output.write(buffer, 0, read);
Log.d("output", output.toString());
}
output.flush();
output.close();
is.close();
Log.d("fileName", filename.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
重现问题
要清楚地确定是否有其他原因导致您的文件无法创建,并假设您未获得 root 权限,请尝试在 SD 卡(外部存储)中创建文件。
如果您没有root,您将无法通过浏览来查看该文件。您可以使用代码读取文件并记录其内容,并且还记得捕获异常,以防文件不存在。这将帮助您知道它可能尚未创建。
关于java - android 内部存储写入文件后没有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25437205/