gpt4 book ai didi

java - 请求权限对话框未显示

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:10 26 4
gpt4 key购买 nike

我正在尝试在 Android 应用程序上请求 READ_EXTERNAL_STORAGE 的权限。我的问题是没有显示对话框,因此也永远不会调用“onRequestPermissionsResult”。我在这里做错了什么吗?我点击了这个链接:Developer Android

这是我的代码:

public class AccountActivity extends AppCompatActivity {

protected TextView changeProfileImgBtn;
private static final int SELECTED_PICTURE = 1;
private static final int F = 101;

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

changeProfileImgBtn = (TextView) findViewById(R.id.accountChangeProfileImgBtn);

changeProfileImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (ContextCompat.checkSelfPermission(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
System.out.println("shouldShowRequestPermissionRationale");

} else {
ActivityCompat.requestPermissions(AccountActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
F);

System.out.println("REQUEST");

}
} else {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
}
}
});
}

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

System.out.println("onRequestPermissionsResult");
switch (requestCode) {
case F: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

System.out.println("GRANTED");

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);

} else {
System.out.println("DENIED");
}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}
}

正如您在日志中看到的那样,ActivityCompat.requestPermissions(...) 在打印“REQUEST”时被调用。无论我运行的设备如何,“shouldShowRequestPermissionRationale”都不会被打印。 “onRequestPermissionsResult”都不会被打印。

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/System.out: REQUEST
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ee9f27b time:106175669

我已将其包含在我的 list 文件中:

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

非常感谢任何帮助。

最佳答案

更新:您的 list 中是否请求了权限?您只能请求 list 中的权限。

此处的文档:https://developer.android.com/guide/topics/manifest/manifest-intro.html

关于java - 请求权限对话框未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40241078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com