Paul Man
Paul Man

Reputation: 151

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. 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.

Here is my manifest, and I also get permission at the beginning of the 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)

Upvotes: 0

Views: 378

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006634

getPath() on a Uri only has meaning if the scheme is file. Your scheme is content.

Use getContentResolver().openInputStream() to get an InputStream on the content. This works for both file and content schemes.

Upvotes: 1

Related Questions