[英]Unable to find file after recording audio on Android
I want to record my voice, store that to a file, then encode the file to base64 string. 我想记录我的声音,将其存储到文件中,然后将文件编码为base64字符串。 So I use the built in recorded using intent like this:
所以我使用这样的内置记录使用意图:
Intent recSound= new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(recSound, RESULT_CAPTURE_AUDIO);
The built in audio recorder pops up, and I record my voice, then when I exit the recorded, it goes back to my application calling this function: 弹出内置录音机,我录制我的声音,然后当我退出录音时,它返回到我的应用程序,调用此函数:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == RESULT_OK) && (requestCode == RESULT_CAPTURE_AUDIO)) {
uri = data.getData();
File f = new File(uri.getPath());
try {
byte[] bytes;
bytes = FileUtils.readFileToByteArray(f); << < crashes here
String b64 = Base64.encodeToString(bytes, Base64.URL_SAFE + Base64.NO_WRAP);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The URI looks fine and I can even play back the URI, but when I try to read the bytes from the URI converted to a path so that I can convert it to base64, there is an exception thrown telling that the file doesn't exist. URI看起来不错,我什至可以播放URI,但是当我尝试从URI读取转换为路径的字节以便可以将其转换为base64时,抛出异常,表明该文件不存在。
Here is my manifest, and I also get permission at the beginning of the MainActivity: 这是我的清单,并且在MainActivity的开头也获得了许可:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Here is the exception. 这是例外。 The exact code is not shown above because it's complicated so it may not match what you see exactly in the exception.
上面没有显示确切的代码,因为它很复杂,因此可能与您在异常中看到的完全不匹配。
07-16 19:38:37.854 W/System.err: java.io.FileNotFoundException: File '/internal/audio/media/37' does not exist
07-16 19:38:37.864 V/InputMethodManager: focusIn: android.support.v4.widget.DrawerLayout{41b3ee08 VFE..... .F...... 0,84-540,922 #7f0d0091 app:id/drawerLayout}
07-16 19:38:37.864 W/System.err: at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:136)
07-16 19:38:37.864 W/System.err: at org.apache.commons.io.FileUtils.readFileToByteArray(FileUtils.java:994)
07-16 19:38:37.864 W/System.err: at com.example.ns.app.MySendImageAsync.doInBackground(MySendImageAsync.java:88)
07-16 19:38:37.884 W/System.err: at com.example.ns.app.MySendImageAsync.doInBackground(MySendImageAsync.java:27)
07-16 19:38:37.894 W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-16 19:38:37.894 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-16 19:38:37.894 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-16 19:38:37.904 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-16 19:38:37.904 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-16 19:38:37.904 W/System.err: at java.lang.Thread.run(Thread.java:841)
getPath()
on a Uri
only has meaning if the scheme is file
. 仅当方案为
file
Uri
上的getPath()
才有意义。 Your scheme is content
. 你的计划很
content
。
Use getContentResolver().openInputStream()
to get an InputStream
on the content. 使用
getContentResolver().openInputStream()
获取内容上的InputStream
。 This works for both file
and content
schemes. 这适用于
file
和content
方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.