Android 弹出相册权限实现流程

整体流程概述

  1. 检查相册权限是否已经获取;
  2. 如果权限已经获取,则弹出相册选择界面;
  3. 如果权限未获取,则向用户申请相册权限;
  4. 用户授权后,弹出相册选择界面;
  5. 处理相册选择结果。

详细步骤及代码实现

1. 检查相册权限是否已经获取

在AndroidManifest.xml文件中添加相册权限声明,示例代码如下:

<manifest>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <!-- 其他权限声明 -->
</manifest>

在Activity中的onCreate方法中添加以下代码,用于检查相册权限是否已经获取:

private boolean checkStoragePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    return result == PackageManager.PERMISSION_GRANTED;
}

2. 弹出相册选择界面

如果权限已经获取,直接跳转到相册选择界面。根据具体需求,可以使用系统相册或自定义相册选择器。

private void openAlbum() {
  // 跳转到系统相册选择界面
  Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(intent, REQUEST_CODE_SELECT_ALBUM);
}

3. 申请相册权限

如果权限未获取,需要向用户申请相册权限。示例代码如下:

private void requestStoragePermission() {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION);
}

同时,需要处理权限申请结果,在Activity中重写onRequestPermissionsResult方法:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 用户授权,弹出相册选择界面
            openAlbum();
        } else {
            // 用户拒绝授权,弹出提示框或其他操作
            Toast.makeText(this, "需要相册权限才能选择图片", Toast.LENGTH_SHORT).show();
        }
    }
}

4. 处理相册选择结果

在Activity中重写onActivityResult方法,处理相册选择结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_SELECT_ALBUM && resultCode == RESULT_OK) {
        if (data != null) {
            Uri uri = data.getData();
            // 处理选择的图片
            handleSelectedImage(uri);
        }
    }
}

代码整体示例

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_SELECT_ALBUM = 1;
    private static final int REQUEST_CODE_STORAGE_PERMISSION = 2;

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

        if (checkStoragePermission()) {
            // 相册权限已经获取,直接打开相册选择界面
            openAlbum();
        } else {
            // 相册权限未获取,申请相册权限
            requestStoragePermission();
        }
    }

    private boolean checkStoragePermission() {
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    private void requestStoragePermission() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION);
    }

    private void openAlbum() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUEST_CODE_SELECT_ALBUM);
    }

    private void handleSelectedImage(Uri uri) {
        // 处理选择的图片
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授权,弹出相册选择界面
                openAlbum();
            } else {
                // 用户拒绝授权,弹出提示框或其他操作
                Toast.makeText(this, "需要相册权限才能选择图片", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SELECT_ALBUM && resultCode == RESULT_OK