1、添加依赖
dependencies {
implementation 'pub.devrel:easypermissions:3.0.0'
}
2、在AndroidManifest文件中添加需要的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3、复写onRequestPermissionsResult方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 将返回结果转给EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
4、检查权限
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// 已获取权限
// ...
} else {
// 没有权限,现在去获取
// ...
}
5、申请权限
public static final int RC_READ_EXTERNAL_STORAGE = 1; // requestCode
EasyPermissions.requestPermissions(this, "申请内存权限",
RC_READ_EXTERNAL_STORAGE, perms);
6、实现EasyPermissions.PermissionCallbacks接口,处理申请结果
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
// 一些权限被授予
Toast.makeText(this, "允许", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
// 一些权限被禁止
Toast.makeText(this, "禁止", Toast.LENGTH_SHORT).show();
}
}
7、如果用户点击了拒绝,并勾选了不再询问
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
//弹出个对话框 可以自定义
}
8、完整的Activity活动如下
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import net.sourceforge.pinyin4j.PinyinHelper;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
public class MainActivity2 extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{
private TextView ta;
private ImageView ig;
int REQUEST_IMAGE_OPEN = 2;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 将返回结果转给EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
// 一些权限被授予
Toast.makeText(this, "允许", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
// 一些权限被禁止
Toast.makeText(this, "禁止", Toast.LENGTH_SHORT).show();
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
//弹出个对话框 可以自定义
}
}
public static final int RC_READ_EXTERNAL_STORAGE = 1; // requestCode
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ta = (TextView) findViewById(R.id.ta);
ig = (ImageView) findViewById(R.id.ig);
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// 已获取权限
// ...
Toast.makeText(this, "已获取权限", Toast.LENGTH_SHORT).show();
} else {
// 没有权限,现在去获取
// ...
Toast.makeText(this, " 没有权限,现在去获取", Toast.LENGTH_SHORT).show();
EasyPermissions.requestPermissions(this, "申请内存权限",
RC_READ_EXTERNAL_STORAGE, perms);
}
ta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//打开相册
Intent intent = new Intent(Intent.ACTION_PICK);
//指定获取的是图片
intent.setType("image/*");
startActivityForResult(intent, REQUEST_IMAGE_OPEN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//将选择的图片显示
if(requestCode == REQUEST_IMAGE_OPEN && resultCode == Activity.RESULT_OK &&data !=null){
Uri uris;
uris = data.getData();
Bitmap bitmap = null;
//Uri转化为Bitmap
try {
bitmap = getBitmapFromUri(uris);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ig.setImageBitmap(bitmap);
}
}
//Uri转化为Bitmap
private Bitmap getBitmapFromUri(Uri uri) throws FileNotFoundException {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
return bitmap;
}
}