如何获得READ_EXTERNAL_STORAGE运行时权限,以便在android studio中为我的应用工作?

ee7vknir  于 2023-05-18  发布在  Android
关注(0)|答案(6)|浏览(347)

我试图添加代码,以请求用户在运行时的权限读取_external_storage,我在过去的构建gradle文件中使用了dexter实现,并在我的www.example.com中添加了代码MainActivity.java,但这不再工作。有谁知道在android studio中,对于21到32之间的sdk,当前和最新的请求read_external_storage运行时权限的方法是什么?

cpjpxq1n

cpjpxq1n1#

您需要预先在android manifest文件中设置权限,使用<uses-permission>,如果您需要访问外部存储,可能使用<uses-feature>
此外,Android已经迁移到Kotlin和Jetpack / Jetpack Compose。遗留代码将工作...但现在情况完全不同了特别是有权限的。你不再要求他们了。您可以在Android Manifest文件中宣布它们。此外,Android限制了允许写入外部存储的最大Android版本。所以Manifest文件需要调用一个工具:

<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  tools:remove="android:maxSdkVersion" />
htrmnn0y

htrmnn0y2#

正确的方法是

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

在清单文件中。有时候你需要,

android:requestLegacyExternalStorage="true"

也在清单文件中。
还有

ActivityCompat.requestPermissions(this,
            new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
            PackageManager.PERMISSION_GRANTED);

在java文件中。

mtb9vblg

mtb9vblg3#

从这里
注意:从API级别33开始,此权限无效。如果您的应用访问其他应用的媒体文件,请改为请求以下一个或多个权限:READ_MEDIA_IMAGES、READ_MEDIA_VIDEO、READ_MEDIA_AUDIO。了解有关与媒体文件关联的存储权限的详细信息。

<!-- Required only if your app needs to access images or photos
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<!-- Required only if your app needs to access videos
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Required only if your app needs to access audio files
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

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

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

zzoitvuj4#

请使用此代码段包含外部存储权限

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
        arrayOf(
           Manifest.permission.READ_MEDIA_IMAGES,
           Manifest.permission.READ_MEDIA_VIDEO,
           Manifest.permission.READ_MEDIA_AUDIO
        ) 
      }
      else {
        arrayOf(
         Manifest.permission.WRITE_EXTERNAL_STORAGE,
         Manifest.permission.READ_EXTERNAL_STORAGE
        )
      }

这将是安全的,当涉及到新的Android API。

pokxtpni

pokxtpni5#

要向用户请求READ_EXTERNAL_STORAGE权限,首先需要在清单文件中添加权限,如下所示:

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

然后在Activity.java文件中,需要添加以下代码:

public class Activity extends AppCompatActivity {
    //Create a request code
    public static final int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        //Check if permission has been granted
        if (checkPermissions()) {
            //Your code
        }
        //Otherwise request permission 
        else {
            requestPermissions();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_PERMISSION_CODE:
                if (grantResults.length > 0) {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_LONG).show();
                    } 
                    else {
                        Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_LONG).show();
                    }
                }
                break;
        }
    }

    public boolean checkPermissions() {
        int result = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermissions() {
        ActivityCompat.requestPermissions(Activity.this, new String[]{READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_CODE);
    }
}

您还需要添加以下导入:

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
ht4b089n

ht4b089n6#

步骤1:添加android manifest.xml的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
步骤2:检查权限,必要时向用户请求权限:

if(ContextCompat.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
        Manifest.permission.READ_EXTERNAL_STORAGE)) {
 
}

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

return;

}
步骤3:回调

public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Permission granted.
    } else {
        //  Permission Refused.
    }
}
}

相关问题