访问相册只在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;
}
}