将文件复制到 Android 设备内部区域需要清单权限吗?
我想让用户选择将文件复制到 Android 设备数据目录以及外部区域。
您能告诉我需要在清单中添加哪些内容才能允许我的应用程序将文件复制到内部区域吗?
我计划将文件存储在 /data/data/your.app.package.name/myFiles 中。在代码中,File.DirInternal 转换为该位置。
如果我使用 File.DirRootExternal,它会转到 /mnt/sdcard,这不会使应用程序崩溃。
这就是为什么我认为清单导致了问题。
如果我使用 File.DirInternal,它会在这一行崩溃:
mpTheMediaPlayer.Load(File.DirInternal, "114.mp3")
这是崩溃的代码:
' 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
这是日志中的错误:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关详细信息,请参阅 Android 的数据存储 文档保存到内部存储器。
如果你的意思是:
那么你不需要额外的权限。这只是定期写入内部存储
如果您指的是外部存储(它看起来确实不像)那么您需要
如果您真正的意思
那么答案是如果没有 root 就不能。您无权写入 /data 文件夹。
Take a look at Android's Data Storage doc for details on saving to internal storage.
If you meant:
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
If you truly meant
Then the answer is that you can't without being rooted. You don't have permissions to write to the /data folder.