File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Writing an audio file in internal memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Writing an audio file in internal memory" Watch "Writing an audio file in internal memory" New topic
Author

Writing an audio file in internal memory

Hardik Trivedi
Ranch Hand

Joined: Jan 30, 2010
Posts: 252
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

Joined: Jan 02, 2010
Posts: 56

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

Joined: Jan 30, 2010
Posts: 252
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

Joined: Jan 02, 2010
Posts: 56

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

Joined: Oct 12, 2008
Posts: 1369
    
    1

Can you post the exception trace?


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

Also, don't forget to declare that you are using external storage in the AndroidManifest.xml file:



JBoss In Action
Hardik Trivedi
Ranch Hand

Joined: Jan 30, 2010
Posts: 252
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

Joined: Jan 30, 2010
Posts: 252
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

Joined: Jan 30, 2010
Posts: 252
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

Joined: May 14, 2008
Posts: 5845
    
    7

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

Joined: Jan 30, 2010
Posts: 252
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing an audio file in internal memory