将文件复制到 Android 设备内部区域需要清单权限吗?

发布于 2024-12-27 16:46:03 字数 3005 浏览 0 评论 0原文

我想让用户选择将文件复制到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2025-01-03 16:46:03

有关详细信息,请参阅 Android 的数据存储 文档保存到内部存储器。

如果你的意思是:

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

那么你不需要额外的权限。这只是定期写入内部存储

如果您指的是外部存储(它看起来确实不像)那么您需要

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

如果您真正的意思

/data/myApp/myFiles

那么答案是如果没有 root 就不能。您无权写入 /data 文件夹。

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.

长不大的小祸害 2025-01-03 16:46:03
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文