Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing an audio file in internal memory

 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to record an audio from user and want to store it on internal memory where my application is installed.
To access this path I am using

This gives path something like /data/data/<packagename>/files

I append file name in path and set the path while recording.
And also passing the same path while playing the recorded voice.

It gives error in mediaplayer's prepare method.

Let me make a note that if I give path "/sdcard/<filename>" everything works perfect.
But it does not work when I use









 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you can find detailed info on storing data on internal and external storage. BTW, I would use external memory for audio files since they tend to be quite big. Generally speaking devices have more external than internal storage space.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Misha,
thanks a lot for your reply.


Yes I agree that it is advisable to store audio files on external memory.
But requirement forces me to do in internal memory only.

So can you tell me why android is not allowing me to create files in
/data/data/<my package>/files/<myfile.3gp> ?





I am using the above code.
Please Help...
 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a hard-coded path? There's no guaranty an other device will use the same directory structure. If you read the link I mentioned, you'll find the correct way.

1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
2. Write to the file with write().
3. Close the stream with close().

Easy does it, let the system figure out where to put the file.

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the exception trace?
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't forget to declare that you are using external storage in the AndroidManifest.xml file:

 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Misha van Tol wrote:Why are you using a hard-coded path?

Thank you again Misha,

I am not using the hardcoded path.The path which I gave above is taken from the logs.
I am fetching the path this way..


I would go for the FileOutputStream but I want to know that if above code works for sdcard path why it is not working for the above given path.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monu Tripathi wrote:Can you post the exception trace?




Above is the Stacktrace of my code...

This is occuring while I try to play the recorded sound. Because system is not able to generate the recording file on given path.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Johnson wrote:Also, don't forget to declare that you are using external storage in the AndroidManifest.xml file:




I want to write file in Internal Memory. I am confused why should use above permisssion in my manifest file.

Can you explain.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardik Trivedi wrote:I want to write file in Internal Memory. I am confused why should use above permisssion in my manifest file.
Can you explain.

Several posters recommended that you use external memory, such as on an SD card, so this was in addition to what they suggested. I find that too much Android-related help shows code examples and omits mentioning the necessary permissions.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Johnson wrote:
Hardik Trivedi wrote:I find that too much Android-related help shows code examples and omits mentioning the necessary permissions.


Yup very true,
But right now I am concentrating to use internal memory only.

Now I am using file descriptor to record and play the sound.
In my application further I weant to use this sound file as a notification sound.I tried to assign notification.sound variable but it accepts only path in string form. So again same problem is arised.

I record the file using file descriptor and using below line to assign path in notification.sound


This line is also returining the same path.And mediaplayer.prepare() method fails for this path.

This is now proving hair raising for me.

Please get me out of this situation.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic