Android权限申请库——EasyPermissions使用详解和打开相册方法

时间:2025-01-30 11:47:34
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; } }