如何在 Android 设备上添加文件访问权限

huangapple go评论80阅读模式
英文:

How to add files access permission on device android

问题

问题是在设备上没有获得文件许可。 下面的图像显示:照片和媒体访问显示出来,但文件访问没有显示。

在上面的权限访问中,文件许可 没有像下面这样显示:我需要包括文件访问在内的以下许可。

我在清单中使用了权限:

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

从 MainActivity 中:

private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;

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

    storage = findViewById(R.id.storage);
    camera = findViewById(R.id.camera);

    // 设置按钮的点击监听器
    storage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            checkPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    STORAGE_PERMISSION_CODE);
        }
    });

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            checkPermission(Manifest.permission.CAMERA,
                    CAMERA_PERMISSION_CODE);
        }
    });
}

// 检查和请求权限的函数
public void checkPermission(String permission, int requestCode)
{
    if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
            == PackageManager.PERMISSION_DENIED) {

        // 请求权限
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[] { permission },
                requestCode);
    }
    else {
        Toast.makeText(MainActivity.this,
                "已经授予权限",
                Toast.LENGTH_SHORT)
                .show();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    super
            .onRequestPermissionsResult(requestCode,
                    permissions,
                    grantResults);

    if (requestCode == CAMERA_PERMISSION_CODE) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                    "已授予相机权限",
                    Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                    "已拒绝相机权限",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    }
    else if (requestCode == STORAGE_PERMISSION_CODE) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                    "已授予存储权限",
                    Toast.LENGTH_SHORT)
                    .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                    "已拒绝存储权限",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

最后部分的重复方法已省略。

英文:

My problem is when I am not getting files permission in the device.
Below image is showing: photos and media access are showing but files access not showing.

如何在 Android 设备上添加文件访问权限

In the above permission access files permission not showing like below: I need below permissions including files access.

如何在 Android 设备上添加文件访问权限

I use permision in the manifest:

&lt;uses-permission
android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission
android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
&lt;uses-permission
android:name=&quot;android.permission.CAMERA&quot; /&gt;

From the MainActivity:

private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.camera);
// Set Buttons on Click Listeners
storage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE);
}
});
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
checkPermission(Manifest.permission.CAMERA,
CAMERA_PERMISSION_CODE);
}
});
}
// Function to check and request permission.
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
== PackageManager.PERMISSION_DENIED) {
// Requesting the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { permission },
requestCode);
}
else {
Toast.makeText(MainActivity.this,
&quot;Permission already granted&quot;,
Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults)
{
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length &gt; 0
&amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
&quot;Camera Permission Granted&quot;,
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
&quot;Camera Permission Denied&quot;,
Toast.LENGTH_SHORT)
.show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length &gt; 0
&amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
&quot;Storage Permission Granted&quot;,
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
&quot;Storage Permission Denied&quot;,
Toast.LENGTH_SHORT)
.show();
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length &gt; 0
&amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
&quot;Storage Permission Granted&quot;,
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
&quot;Storage Permission Denied&quot;,
Toast.LENGTH_SHORT)
.show();
}
}
}

答案1

得分: 1

build.gradle文件中的targetSdkVersion从30更改为29后,我的对话框从“照片/媒体”更改为“照片/媒体/文件”。我相信这与API 30在作用域存储方面更加严格有关。

英文:

Changing the targetSdkVersion from 30 to 29 in my build.gradle file changed the dialog for me from photos/media to photos/media/files. I believe it has something to do with how API 30 is more strict with scoped storage.

答案2

得分: 0

在清单文件的应用程序部分中添加以下行:

android:requestLegacyExternalStorage="true"

可以解决这个问题。

英文:

Adding the below line in the Application portion of Manifest

android:requestLegacyExternalStorage=&quot;true&quot;

solves the problem.

huangapple
  • 本文由 发表于 2020年9月29日 16:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64115729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定