3

I would like to give the user the choice to copy files onto the Android device data directory as well as the external area.

Can you tell me what I need to add to the manifest that will allow my app to copy the files to the internal area?

I was planning on storing the files in /data/data/your.app.package.name/myFiles. In the code, File.DirInternal translates to that location.

If I use File.DirRootExternal, it goes to /mnt/sdcard which does not crash the app.

That's why I thought the manifest was causing the issue.

It crashes on this line if I use File.DirInternal:

mpTheMediaPlayer.Load(File.DirInternal, "114.mp3")

This is the code that's crashing:

' Http sub routines.
'-------------------
Sub JobDone(Job As String)

Select Job
    Case "Job1"

        If HttpUtils.IsSuccess(mp3Url) Then 
            Dim blnFolderMade As Boolean

            mp3 = HttpUtils.GetInputStream(mp3Url)

            timer1.Enabled = True

            Dim out As OutputStream
            out = File.OpenOutput(File.DirInternal,"114.mp3",False)
            File.Copy2(mp3, out)
            out.Close

            ProgressBarDownloading.Visible = False

            ToggleButtonPlayPause.Checked = True
            ToggleButtonPlayPause.Enabled = True
            blnCurrentPauseCheckedState = True
            blnCurrentPauseEnabledState = True

            mpTheMediaPlayer.Release
            mpTheMediaPlayer.Initialize2("")
            mpTheMediaPlayer.Load(File.DirInternal, "114.mp3")
            mpTheMediaPlayer.Play
        End If
End Select
End Sub

Here is the error from the logs:

Starting Job: Job1
** Service (httputilsservice) Create **
** Service (httputilsservice) Start **
main_jobdone (java line: 518)
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer.prepare(Native Method)
at anywheresoftware.b4a.objects.MediaPlayerWrapper.Load(MediaPlayerWrapper.java:79)
at quran.repeater.main._jobdone(main.java:518)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:97)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:772)
at anywheresoftware.b4a.keywords.Common.CallSub2(Common.java:759)
at quran.repeater.httputilsservice._processnexttask(httputilsservice.java:180)
at quran.repeater.httputilsservice._response_streamfinish(httputilsservice.java:244)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
at anywheresoftware.b4a.BA$1.run(BA.java:218)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.io.IOException: Prepare failed.: status=0x1
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

2 Answers2

3

Take a look at Android's Data Storage doc for details on saving to internal storage.

If you meant:

/data/data/your.app.package.name/myFiles

Then you don't need extra permissions. That's just regular writing to internal storage

If you meant external storage (which it sure doesn't look like) then you need

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you truly meant

/data/myApp/myFiles

Then the answer is that you can't without being rooted. You don't have permissions to write to the /data folder.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Yes, I'm trying to do the copy to /data/data/your.app.package.name/myFiles but I get a "prepare failed" error. I'm trying to copy a file that I use the media player to play. Also the device I'm using is rooted but I'm not sure if the user's device will be rooted. – Emad-ud-deen Jan 19 '12 at 18:54
  • Update your original post with your code and the exception with stack trace, and I'll take a look. – kabuko Jan 19 '12 at 22:06
  • Thanks for the reply. I updated the post with the code and logs. – Emad-ud-deen Jan 20 '12 at 10:45
  • 1
    Seems like a problem with MediaPlayer and not storage. See these [other](http://stackoverflow.com/questions/3761305/android-mediaplayer-throwing-prepare-failed-status-0x1-on-2-1-works-on-2-2) [posts](http://stackoverflow.com/questions/6138328/java-io-ioexception-prepare-failed-status-0x1). – kabuko Jan 21 '12 at 04:36
  • Thanks for the replies. Looks like I need to stick with only File.DirRootExternal as the location to put the files as it seems it is the only one the mp3 file will play from so it does look like media player does not like the File.DirInternal location. – Emad-ud-deen Jan 22 '12 at 11:57
2
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
dcanh121
  • 4,665
  • 11
  • 37
  • 84