简体   繁体   English

材料设计和外部存储访问的最低/目标SDK版本应该是什么?

[英]What should be my minimum/target sdk version for material design and external storage acess?

I want to write a very simple app that only has 2 requirements. 我想编写一个只有2个要求的非常简单的应用程序。 Support for material design and uses the phone internal storage. 支持材料设计并使用手机内部存储。

So when I created the Android studio project my minSdkVersion was set to 21 and my targetSdkVersion was set to 26. 因此,当我创建Android Studio项目时,我的minSdkVersion设置为21,而我的targetSdkVersion设置为26。

This gave me the following problem. 这给了我以下问题。 When I wanted to get access to internal storage I couldn't because of the new runtime permission system. 当我想访问内部存储时,由于新的运行时权限系统,我无法访问。 I figured I needed to write the code for requesting persmissions but then I run into the problem that the requestPermissions function requires a minSdkVersion of 23. 我认为我需要编写用于请求权限的代码,但是随后遇到了一个问题,即requestPermissions函数的minSdkVersion必须为23。

So, here is what I want. 所以,这就是我想要的。 I want to make sure that my app works in any android version higher than 5. What is the right way to solve this permission issue and what should my minSdk and targetSdk be? 我想确保我的应用程序可以在高于5的任何android版本中使用。解决此权限问题的正确方法是什么?minSdk和targetSdk应该是什么?

I want to set my minSdkVersion to 21 and be done with it, but I don't know what problems that might bring. 我想将minSdkVersion设置为21并完成它,但是我不知道会带来什么问题。

You should request the permissions using the Support Library. 您应该使用支持库请求权限。 The official documentation explains pretty well the entire process: Requesting Permissions at Run Time . 官方文档很好地解释了整个过程: 在运行时请求权限 As this guide says: 如本指南所述:

using the support library is simpler, since your app doesn't need to check which version of Android it's running on before calling the methods 使用支持库更简单,因为您的应用无需在调用方法之前检查其运行的Android版本

public boolean haveStoragePermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (getApplicationContext().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
                    getApplicationContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                ActivityCompat.requestPermissions(AddPostActivity.this , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else {
            return true;
        }
    }

And add this method at the beginning of onCreate() method and don't forget to add those two permission into mainifest file. 并在onCreate()方法的开头添加此方法,不要忘记将这两个权限添加到mainifest文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该选择什么样的最低SDK版本? (如2018年11月) - What is the minimum SDK version should I choose? (as in Nov 2018) 我应该选择的最低 SDK 版本是多少? (如 2019 年 7 月),为什么? - What is the minimum SDK version should I choose? (as in July 2019) and why? 您应该选择哪个构建目标和最低SDK版本? - What build target and min SDK version should you choose? 通过在带有目标 SDK 29 的 Android 10 上启用传统外部存储,究竟是什么选择退出范围存储 - what exactly opt out from scoped storage by enabling the legacy external storage on Android 10 with target SDK 29 此错误的解决方案是什么(目标 SDK 版本 30 至少需要签名方案 v2) - what the solution of this error ( Target SDK version 30 requires a minimum of signature scheme v2 ) 要设置的值:“Minimum Required SDK”,“Target SDK”和“Compile with” - What value to set for: “Minimum Required SDK” , “Target SDK” & “Compile with” Android,是否可以以不同方式设置项目构建目标和最小SDK版本? - Android, is it possible to set project build target and minimum SDK Version differently? 如果我想使用蓝牙和目标 SDK 31 但我的最低 SDK 是 23,我需要在 Android 上获得什么权限? - What permissions would I need on Android if I want to use Bluetooth and Target SDK 31 but my minimum SDK is 23? EditText线的颜色应该是什么(材料设计) - What should be the color of EditText line (Material Design) android最低和目标SDK - android minimum and target SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM