Can't open an existing file in Sdcard in Android Studio -
i have code in project read audio file :
mainactivity2:
private string getfilename() { string filepath = environment.getexternalstoragedirectory().getpath(); file file = new file(filepath, audio_recorder_folder); if (!file.exists()) { file.mkdirs(); } return (file.getabsolutepath() + "/" + system.currenttimemillis() + file_exts[currentformat]); } private void startrecording() { recorder = new mediarecorder(); recorder.setaudiosource(mediarecorder.audiosource.mic); recorder.setoutputformat(output_formats[currentformat]); recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); recorder.setoutputfile(getfilename()); log.i("tag", getfilename()); listeaudio.put(indice,getfilename()); // here listeaudio path=getfilename(); recorder.setonerrorlistener(errorlistener); recorder.setoninfolistener(infolistener); try { recorder.prepare(); recorder.start(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
mainactivity:
file audiofile = new file( mainactivity2activity.listeaudio.get(z)); // listeaudio return /storage/sdcard0/audiorecorder/1433288052123.mp4 uri audiouri = uri.fromfile(audiofile); vocal = mediaplayer.create(mainactivity.this,audiouri); vocal.start();
that's doesn't work , in logcat there error say: failed open file '/storage/sdcard0/audiorecorder/1433288052123.mp4'. (no such file or directory) , when search manually path in file explorer found audio file wich want play. have in androidmanifest:
<uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.record_audio" />
please help.
what implementation of call mainactivity2activity.listeaudio.get(z)
? try use system api access external storage.
if (environment.getexternalstoragestate().equals( environment.media_mounted)) { file audiofile = new file(environment.getexternalstoragedirectory() .getabsolutefile() + file.separator + "audiorecorder/1433288052123.mp4"); if (audiofile.exists()) { mediaplayer mediaplayer = mediaplayer.create(this, uri.fromfile(audiofile)); mediaplayer.start(); } }
// edit
recorder.setoutputfile(getfilename()); log.i("tag", getfilename()); listeaudio.put(indice,getfilename()); // here listeaudio
the file path write , file path save not same file, getfilename()
return unique file path every time called.
change this, if want use current milliseconds file name.
final string filepath = getfilename(); recorder.setoutputfile(filepath); log.i("tag", filepath); listeaudio.put(indice, filepath); // here listeaudio
Comments
Post a Comment