一天 访问相册只在AndroidManifest.xml中加入权限是不行的 需要加入动态权限

访问相册只在AndroidManifest.xml中加入权限是不行的
需要加入动态权限在这里插入代码片

public class MainActivity extends AppCompatActivity {

private final static String TAG = MainActivity.class.getSimpleName();

private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private int b = 0;


ArrayList<String> data = new ArrayList<>();

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


    boolean checkPer = true;
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {// 没有权限。
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        checkPer = false;
    }
    if(checkPer){
        initData();
        initView();
    }

    Button button = (Button)findViewById( R.id.button );
    button.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (b == 0){
                mRecyclerView.setLayoutManager( new GridLayoutManager( getApplicationContext(),4 ) );
                b = 1;
            }
            else if(b==1){
                mRecyclerView.setLayoutManager( new GridLayoutManager( getApplicationContext(),3 ) );
                b = 2;
            }
            else if(b==2){
                mRecyclerView.setLayoutManager( new GridLayoutManager( getApplicationContext(),2 ) );
                b = 0;
            }
        }
    } );
    Button button1 = (Button)findViewById( R.id.button2 );
    button1.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (b == 0){
                a =2;
                initView2();
                b = 1;
            }
            else if(b==1){
                a =3;
                initView2();
                b = 2;
            }
            else if(b==2){
                a =4;
                initView2();
                b = 0;
            }
        }
    } );

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 100: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被用户同意。
                initData();
                initView();
            } else {
                // 权限被用户拒绝了。
                finish();
            }
            return;
        }
    }
}

@SuppressLint("WrongConstant")
private void initData() {
    mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    mAdapter = new MyAdapter(MainActivity.this, getData());
}

private void initView() {
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    StaggeredGridLayoutManager layoutManager = new
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    // 设置布局管理器
    mRecyclerView.setLayoutManager(layoutManager);
    // 设置adapter
    mRecyclerView.setAdapter(mAdapter);
}

int a = 2;
private void initView2() {
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    StaggeredGridLayoutManager layoutManager = new
            StaggeredGridLayoutManager(a,StaggeredGridLayoutManager.VERTICAL);
    layoutManager.setOrientation( LinearLayoutManager.HORIZONTAL );
    // 设置布局管理器
    mRecyclerView.setLayoutManager(layoutManager);
    // 设置adapter
    mRecyclerView.setAdapter(mAdapter);
}

  public ArrayList<String> getData() {

      Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      String[] projection = new String[] { MediaStore.Images.Media._ID,
              MediaStore.Images.Media.BUCKET_ID, // 直接包含该图片文件的文件夹ID,防止在不同下的文件夹重名
              MediaStore.Images.Media.BUCKET_DISPLAY_NAME, // 直接包含该图片文件的文件夹名
              MediaStore.Images.Media.DISPLAY_NAME, // 图片文件名
              MediaStore.Images.Media.DATA // 图片绝对路径
      };
      Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
      if (cursor != null) {
          Log.d(TAG,"cursor count="+cursor.getCount());
          while (cursor.moveToNext()) {
              int index = cursor
                      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              String path = cursor.getString(index); // 文件地址
              File file = new File(path);
              if (file.exists()) {
                  data.add(path);
              }
          }
          cursor.close();
      }
      return data;
  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值