清單文件添加權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
MainActivity布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/Button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取圖庫"
/>
<GridView
android:id="@+id/GridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
>
</GridView>
</LinearLayout>
MainActivity代碼
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private List<String> list=new ArrayList<>();
private Button Button_photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
gridView=findViewById(R.id.GridView);
Button_photo=findViewById(R.id.Button_photo);
Button_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if(cursor!=null){
while(cursor.moveToNext()){
byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
list.add(new String(data,0,data.length-1));
}
MyAdapter myAdapter = new MyAdapter(list,MainActivity.this);
gridView.setAdapter(myAdapter);
}
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
View view1 = getLayoutInflater().inflate(R.layout.myview, null);
ImageView ImageView = view1.findViewById(R.id.ImageView_look);
ImageView.setImageBitmap(BitmapFactory.decodeFile(list.get(i)));
new AlertDialog.Builder(MainActivity.this).setView(view1).setPositiveButton("確定",null).show();
}
});
}
}
適配器代碼
public class MyAdapter extends BaseAdapter {
private List<String> list;
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHoder viewHoder;
if(view==null){
view=layoutInflater.inflate(R.layout.item,null);
viewHoder=new ViewHoder();
viewHoder.imageView=view.findViewById(R.id.ImageView);
view.setTag(viewHoder);
}
else{
viewHoder = (ViewHoder) view.getTag();
}
Glide.with(context).load(list.get(i)).into(viewHoder.imageView);
return view;
}
class ViewHoder{
ImageView imageView;
}
}
item布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
myview布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ImageView_look"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>