简体   繁体   English

READ_EXTERNAL_STORAGE权限,尽管清单中有此权限,但应用程序崩溃

[英]READ_EXTERNAL_STORAGE permission, app crash despite I have this in manifest

I have problem with permissions. 我的权限有问题。 What should I do to stop my app from crashing? 我该怎么做才能阻止我的应用崩溃?

MainActivity.java MainActivity.java

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

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == RESULT_OK  && data != null){
        Uri selectedImage = data.getData();
        try {
            Bitmap bitmap =  MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
            ImageView imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

AndroidManifest.xml AndroidManifest.xml

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

I just added this in my OnCreate method 我刚刚在我的OnCreate方法中添加了

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

And it seems to work, is there any better solution? 似乎可行,有没有更好的解决方案?

If putting in your onCreate() works I would check whether you put it in the manifest in the correct place because that has caused me issues in the past. 如果放入您的onCreate()有效,我将检查您是否将其放在清单中的正确位置,因为这过去曾引起我问题。

Make sure <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> is after the first manifest closing tag and before the first <Application ... 确保<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />在第一个清单关闭标记之后,并且在第一个<Application ...之前<Application ...

For example: 例如:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.mypackagename">

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

    <application...

try this : 尝试这个 :

  //check permission is granted or no    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) 
     {
      requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, RESULT_LOAD_IMG);
     } 
    else
     {
    //your code
     }

If your target build version is API 23 or higher, just check that the permission is granted or not. 如果您的目标构建版本是API 23或更高版本,则只需检查是否已授予许可。 Go to Settings -> Apps and then tap on your app. 转到设置->应用,然后点击您的应用。 In Permissions option enable the Storage permission. 在“权限”选项中,启用“存储”权限。 But it is not the permanent solution. 但这不是永久的解决方案。

Well i do something like u want. 好吧,我做了你想要的事情。 I give u simply code how to do this :) 我给你简单的代码如何做到这一点:)

Here i checking permission(i call this method in fragment, make this in activity) 在这里,我检查权限(我在片段中调用此方法,使其处于活动状态)

@TargetApi(23)
public void checkStoragePermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (settingsDrawerFragment != null) {
            settingsDrawerFragment.onPermissionGranted();
        }
        return;
    }
    if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
            .PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_CODE_EXTERNAL_STORAGE);
        return;
    }
    if (settingsDrawerFragment != null) {
        settingsDrawerFragment.onPermissionGranted();
    }
}

This check permission with auto message Also i make onRequestPermissions 此检查权限与自动消息还我使onRequestPermissions

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_AUDIO_RECORD:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startBrowseActivity();
            } else {
                Utils.showToast(this, getString(R.string.audio_permission_denied));
            }
            break;
        case REQUEST_CODE_EXTERNAL_STORAGE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (settingsDrawerFragment != null) {
                    settingsDrawerFragment.onPermissionGranted();
                }
            } else {
                if (settingsDrawerFragment != null) {
                    closeSettingsDrawer();
                }
                Utils.showToast(this, getString(R.string.storage_permission_denied));
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}

Remember to make methods in activity. 记住要在活动中创建方法。 On fragment check it. 在片段上检查它。

Finally i make something like this to check permissions in fragment 最后我做了这样的事情来检查片段中的权限

 public void setHomeStreamSource(int position) {
    if (position == STORAGE_CLOUD_CACHE) {
        preferenceHelper.putInt(getString(R.string.preferences_usb_prefs), getString(R.string
                .preferences_video_source), MainActivity.SOURCE_CLOUD);
        closeDrawer();
        Utils.showToast(activity, getString(R.string.source_selected));
    } else if (position == STORAGE_USB) {
        ((MainActivity) activity).checkStoragePermission();
    }
}

Hope this sample code help u well 希望此示例代码对您有帮助

暂无
暂无

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

相关问题 即使我在清单中声明,Read_external_storage 权限也不起作用,不在 android 工作室中运行 - Read_external_storage permission is not working even i have declared in manifest,not running in android studio 由于需要READ_EXTERNAL_STORAGE权限而崩溃,即使它在清单中 - Crash due to requiring READ_EXTERNAL_STORAGE permission even though it's in the Manifest Android READ_EXTERNAL_STORAGE 权限不起作用 - Android READ_EXTERNAL_STORAGE permission not working Android 的 READ_EXTERNAL_STORAGE 权限 - READ_EXTERNAL_STORAGE permission for Android Android - 尽管内联权限和清单权限,从存储中读取文件“Permission Denied” - Android - Read file from storage “Permission Denied” despite inline permissions and manifest permissions READ_EXTERNAL_STORAGE 是否可以根据情况在 Android 11 中提供不同程度的访问权限? - Is it possible for READ_EXTERNAL_STORAGE to give varying degrees of access, in Android 11 depending on the circumstance? 如何在 android 11 上使用没有 READ_EXTERNAL_STORAGE 的 File("/sdcard/myfile")? - How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11? android.Manifest.permission.WRITE_EXTERNAL_STORAGE-应用程序崩溃 - android.Manifest.permission.WRITE_EXTERNAL_STORAGE - application crashes 即使我声明了Android权限,WRITE EXTERNAL STORAGE也不起作用 - Android permission WRITE EXTERNAL STORAGE doesn't work even if I have declared it SmsManager无法正常工作。 我也将许可包括在清单中 - SmsManager not working. I have also included the permission in manifest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM