如何解决将音频保存到SD卡
我一直想将音频保存到SdCard。
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/AUdio/");
boolean success1;
if (!dir.exists()) {
MessageSystem("SAVING FOLDER: " + dir + " -- DON'T EXISTS ");
//CASE NOT - We create
success1 = dir.mkdir();
if (success1) {
// Do something on success
MessageSystem("foldersd: " + dir.toString() + " CREATED!!");
} else {
// Do something else on failure
MessageSystem("foldersd: " + dir.toString() + " NOT CREATED!!");
}
}
我也试图保存到:
File dir = new File(Environment.getExternalStorageDirectory() + "/AUdio/");
在两种情况下都可以,但是保存到内部存储。从来没有SD卡
我使用了getExternalFilesDirs()。但是,如果用户卸载应用程序,则会丢失文件。
我不要。
任何时候它都会创建到内部存储中。
任何解决方案?
我尝试过此操作,可以在SD卡中创建一个文档。和一个空的。现在,我想将MediaRecorder音频保存到其中。但是我需要道路。我怎么能得到它?
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/mpeg");
intent.putExtra(Intent.EXTRA_TITLE,audio_name);
// Optionally,specify a URI for the directory that should be opened in
// the system file picker when your app creates the document.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,pickerInitialUri);
startActivityForResult(intent,CREATE_FILE);
解决方法
您可以尝试一下
public static final String DEFAULT_STORAGE_LOCATION = "/sdcard/AudioRecording";
File dir = new File(DEFAULT_STORAGE_LOCATION);
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception e) {
Log.e("CallRecorder","RecordService::makeOutputFile unable to create directory " + dir + ": " + e);
Toast t = Toast.makeText(getApplicationContext(),"CallRecorder was unable to create the directory " + dir + " to store recordings: " + e,Toast.LENGTH_LONG);
t.show();
return null;
}
} else {
if (!dir.canWrite()) {
Log.e(TAG,"RecordService::makeOutputFile does not have write permission for directory: " + dir);
Toast t = Toast.makeText(getApplicationContext(),"CallRecorder does not have write permission for the directory directory " + dir + " to store recordings",Toast.LENGTH_LONG);
t.show();
return null;
}
-
您可以通过查询系统来获取sd卡的位置,
Environment.getExternalStorageDirectory();
并且不要忘记添加用户权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。