请求 Android 上的写入权限

发布于 2025-01-12 05:12:19 字数 1693 浏览 1 评论 0原文

抱歉,这个问题可能已经被问过多次了,但我正在努力解决不同的 SDK 版本。我的应用程序用作研究工具,仅按 .apk 直接安装在平板电脑上,因此没有应用程序商店。我需要记录用户的答案并将其写入文本文件。我很难请求用户将数据存储在外部存储上。

我将以下行添加到 AndroidManifest.xml:

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

在 MainActivity 中,我在 onCreate 函数中调用 AskForPermissions:

    public void askForPermissions() {
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (result != PackageManager.PERMISSION_GRANTED){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (!Environment.isExternalStorageManager()) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(intent);
                }else{
                    //TODO no clue, what to do here
                }
            }else{
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                    Toast.makeText(this, "Permission needed to store the data. Please allow storage functionality.", Toast.LENGTH_LONG).show();
                } else {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
                }
            }
        }

        createDir();
    }

在 API 版本 30 中,我使用“Intend”,因此会显示带有权限的设置屏幕,我可以手动授予写入权限- 这有效(生成目录,存储数据......)。第一个问题:在什么情况下会发生没有外部存储管理器的情况(因此是 TODO)?

我正在努力解决的是 SDK < 30. 这似乎不起作用。我没有获得权限,并且在应用程序信息中,我无​​法手动授予应用程序写入权限。权限选项被禁用。在这种情况下请求权限的正确方法是什么(最好是直接在应用程序中)?

很抱歉可能出现重复发帖。这里是新手。

Sorry, this question has probably been asked multiple times already, but I am struggling with different SDK versions. My app is used as a research instrument and solely installed per .apk directly on tablets, thus no app store. I need to record the answers from users and write them into a text file. And I am having a hard time requesting the permission from users to store the data on the external storage.

I added the following line to AndroidManifest.xml:

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

In the MainActivity, I call askForPermissions in the onCreate function:

    public void askForPermissions() {
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (result != PackageManager.PERMISSION_GRANTED){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                if (!Environment.isExternalStorageManager()) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(intent);
                }else{
                    //TODO no clue, what to do here
                }
            }else{
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                    Toast.makeText(this, "Permission needed to store the data. Please allow storage functionality.", Toast.LENGTH_LONG).show();
                } else {
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
                }
            }
        }

        createDir();
    }

In API version 30, I use the "Intend" and accordingly the settings screen with the permissions shows up and I can grant writing permissions manually - and this works (directory is generated, data stored ...). First questions: In which cases can it happen that there is no external storage manager (hence the TODO)?

What I am struggling with is SDK < 30. This does not seem to work. I do not get permissions and in the app info, I cannot give the app writing permissions manually. The permissions option is disabled. What is the correct way to request permissions in that case (most preferably directly in the app)?

Sorry for the possible double post. Newbie here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凉风有信 2025-01-19 05:12:19

在清单中尝试一下:

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

Try this in manifest:

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