第一步:引入依赖
def frescoVersion = "2.3.0" implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' //guilde implementation 'com.github.bumptech.glide:glide:4.4.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0' //fresco implementation "com.facebook.fresco:fresco:${frescoVersion}" implementation "com.facebook.fresco:animated-base:${frescoVersion}" implementation "com.facebook.fresco:animated-drawable:${frescoVersion}" implementation "com.facebook.fresco:animated-gif:${frescoVersion}" implementation "com.facebook.fresco:animated-webp:${frescoVersion}" implementation "com.facebook.fresco:webpsupport:${frescoVersion}"
第二步:添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <!-- 访问网络的权限 --> <uses-permission android:name="android.permission.INTERNET"/>
//安卓10.0以上因为沙箱机制,需要在application添加
android:requestLegacyExternalStorage="true"
第三步:初始化
Fresco需要初始化
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fresco.initialize(this); setContentView(R.layout.activity_main); initAbbreviation(); }
private void initAbbreviation() { ImageView img1 = findViewById(R.id.img1); SimpleDraweeView sdv_head = findViewById(R.id.sdv_head); //if语句 没有读写SD卡的权限,就申请权限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { //获取所有图片存入list集合 getGalleryPhotos方法 allPath = new ArrayList<String>(); allPath = getGalleryPhotos(getContentResolver()); //取出首张图片 if (allPath != null && !allPath.isEmpty()) { // picturePath = allPath.get(0); Log.e("TAG","集合图片的长度:"+allPath.size()); String s = allPath.get(0); Log.e("TAG","第一张图片为:"+s); Uri parse = Uri.parse(s); Glide.with(this).load(s).into(img1); sdv_head.setImageURI(parse); } } } //获取所有图片存入list集合返回,MediaStore.Images.Media.DATA中的Images public static ArrayList<String> getGalleryPhotos(ContentResolver resolver) { ArrayList<String> galleryList = new ArrayList<String>(); try { //获取所在相册和相册id final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}; //按照id排序 final String orderBy = MediaStore.Images.Media._ID; //相当于sql语句默认升序排序orderBy,如果降序则最后一位参数是是orderBy+" desc " Cursor imagecursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy); //从数据库中取出图存入list集合中 if (imagecursor != null && imagecursor.getCount() > 0) { while (imagecursor.moveToNext()) { int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); // Log.d("tgw7", "getGalleryPhotos: " + dataColumnIndex); String path = "file://" + imagecursor.getString(dataColumnIndex); Log.d("TAG", "getGalleryPhotos: " + path); galleryList.add(path); } } } catch (Exception e) { e.printStackTrace(); } // 进行反转集合 Collections.reverse(galleryList); return galleryList; }