引入
https://github.com/pengjianbo/GalleryFinal
aar 比 jar 更好,更先進,aar可以改后綴rar,打開會發現它里面有一個jar 還有其他資源文件
添加aar的方法和jar差不多 文件復制到libs目錄 然后在app的build.gradle文件中 多添加 repositories
repositories { flatDir { dirs 'libs' } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile files('libs/butterknife-7.0.1.jar') compile files('libs/glide-3.7.0.jar') compile(name: 'galleryfinal-1.4.8.7', ext: 'aar') compile(name: 'actionsheet-1.1.6', ext: 'aar') }
在application中 設置 ThemeConfig FunctionConfig ImageLoader CoreConfig
這里可以選擇自己用的圖片加載方式
然后就可以在邏輯代碼中調用了,第一個參數是請求碼,回調時可以用,第二個參數是功能配置,如果不設置就默認用application中的配置,第三個是回調
GalleryFinal.openGallerySingle(200,mOnHanlderResultCallback); FunctionConfig functionConfig=new FunctionConfig.Builder().setEnableCamera(true).setEnableEdit(true) .setEnableCrop(true).setEnableRotate(true).setEnablePreview(true).setForceCrop(true).setForceCropEdit(true).build(); GalleryFinal.openCamera(100,functionConfig,mOnHanlderResultCallback);
回調 成功回調onHanlderSuccess 返回請求碼i 和 list,可以針對不同的請求碼作出不同的操作,這里只選了一張圖片,所以直接get(0),再用getPhotoPath()方法得到路徑
universalimageloader 加載時前面還要加上 "file:/" ,Glide 則不用,因為源碼給的Glide 加載方案中已經加上了 "file:/"
三星手機上 用相機拍出來的照片 用 universalimageloader 加載是 斜的,用Glide 加載就是正常的。其他手機沒有這個問題,所以有必要設置一下 功能配置 setForceCropEdit(true) 強制對圖片進行編輯:旋轉一下
private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback=new GalleryFinal.OnHanlderResultCallback() { @Override public void onHanlderSuccess(int i, List<PhotoInfo> list) { if (list!=null){ if (i==100){ Logger.e("openCamera"); }else if (i==200){ Logger.e("openGallerySingle"); } DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnFail(R.drawable.ic_gf_default_photo) .showImageForEmptyUri(R.drawable.ic_gf_default_photo) .showImageOnLoading(R.drawable.ic_gf_default_photo).build(); ImageLoader.getInstance().displayImage("file:/" + list.get(0).getPhotoPath(), imageView, options); ImageLoader.getInstance().displayImage("file:/" + list.get(0).getPhotoPath(), shapedImageView, options); Glide.with(mContext).load(list.get(0).getPhotoPath()).into(imageView2); final BmobFile bmobFile=new BmobFile(new File(list.get(0).getPhotoPath())); bmobFile.uploadblock(new UploadFileListener() { @Override public void done(BmobException e) { if (e==null){ showToast("上傳文件成功:" + bmobFile.getFileUrl()); Glide.with(mContext).load(bmobFile.getFileUrl()).into(ivbmob); }else { showToast("上傳文件失敗:" + e.getMessage()); } } }); } } @Override public void onHanlderFailure(int i, String s) { showToast(s); } };