本文主要是介绍Android Androidx Glide下载图片到指定文件夹 Glide版本4.+ Glide4.+,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android Androidx Glide下载图片到指定文件夹 Glide版本4.+ Glide4.+
环境:Android Studio 3.6.2
api ("com.github.bumptech.glide:glide:4.11.0") {exclude group: 'com.android.support'}annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'implementation 'com.squareup.retrofit2:2.2.0'implementation 'com.yanzhenjie:permission:2.0.3'
官方文档:http://bumptech.github.io/glide/doc/options.html
1、申请读取权限相关
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Space
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.yanzhenjie.permission.AndPermissionobject PermissionHelper {fun hasPermission(context: Context, vararg permissions: String): Boolean {return AndPermission.hasPermissions(context, *permissions)}fun request(activity: FragmentActivity, callback: PermissionCallback, vararg permission: String) {if (hasPermission(activity, *permission)) {callback.onSuccess()return}val manager = activity.supportFragmentManagervar fragment: Fragment? = manager.findFragmentByTag("permission")if (fragment == null) {fragment = PermissionDelegate()}if (!fragment.isAdded) {val finalFragment = fragmentmanager.beginTransaction().add(fragment, "permission").runOnCommit { (finalFragment as PermissionDelegate).request(callback, *permission) }.commit()} else {(fragment as PermissionDelegate).request(callback, *permission)}}class PermissionDelegate : Fragment() {private var callback: PermissionCallback? = nulloverride fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {return Space(activity)}internal fun request(callback: PermissionCallback, vararg permissions: String) {this.callback = callbackif (isAdded && !isDetached) {AndPermission.with(this).runtime().permission(arrayOf(*permissions)).onGranted {callback.onSuccess()}.onDenied {if (AndPermission.hasAlwaysDeniedPermission(this, it)) {this.activity?.let { SettingDialog(it).show() }}}.start()}}}interface PermissionCallback {fun onSuccess()}}
申请权限拒绝弹框
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;import java.util.Objects;public class SettingDialog {private AlertDialog.Builder mBuilder;private FragmentActivity context;public SettingDialog(@NonNull FragmentActivity context) {mBuilder = new AlertDialog.Builder(Objects.requireNonNull(context)).setCancelable(false).setTitle("权限申请失败").setMessage("我们需要的一些权限被您拒绝或者系统发生错误申请失败,请您到设置页面手动授权,否则功能无法正常使用!").setPositiveButton("去设置", mClickListener).setNegativeButton("取消", mClickListener);this.context = context;}@NonNullpublic SettingDialog setTitle(@NonNull String title) {mBuilder.setTitle(title);return this;}@NonNullpublic SettingDialog setTitle(@StringRes int title) {mBuilder.setTitle(title);return this;}@NonNullpublic SettingDialog setMessage(@NonNull String message) {mBuilder.setMessage(message);return this;}@NonNullpublic SettingDialog setMessage(@StringRes int message) {mBuilder.setMessage(message);return this;}@NonNullpublic SettingDialog setNegativeButton(@NonNull String text, @Nullable DialogInterface.OnClickListenernegativeListener) {mBuilder.setNegativeButton(text, negativeListener);return this;}@NonNullpublic SettingDialog setNegativeButton(@StringRes int text, @Nullable DialogInterface.OnClickListenernegativeListener) {mBuilder.setNegativeButton(text, negativeListener);return this;}@NonNullpublic SettingDialog setPositiveButton(@NonNull String text) {mBuilder.setPositiveButton(text, mClickListener);return this;}@NonNullpublic SettingDialog setPositiveButton(@StringRes int text) {mBuilder.setPositiveButton(text, mClickListener);return this;}public void show() {mBuilder.show();}private DialogInterface.OnClickListener mClickListener = new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {switch (which) {case DialogInterface.BUTTON_NEGATIVE:break;case DialogInterface.BUTTON_POSITIVE:Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);Uri uri = Uri.fromParts("package", context.getPackageName(), null);intent.setData(uri);startForResult(context, intent, 1);break;}}};private static void startForResult(Object object, Intent intent, int requestCode) {if (object instanceof Activity) {((Activity) object).startActivityForResult(intent, requestCode);} else if (object instanceof Fragment) {((Fragment) object).startActivityForResult(intent, requestCode);} else if (object instanceof android.app.Fragment) {((android.app.Fragment) object).startActivityForResult(intent, requestCode);}}
}
2.保存图片核心方法
fun (val activity:Activity){PermissionHelper.request(activity as FragmentActivity, object : PermissionHelper.PermissionCallback {override fun onSuccess() {val urlImage = "https://static.veer.com/veer/static/resources/keyword/2020-02-19/b94a42fb11d64052ae5a9baa25f5370c.jpg"val file = File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "图片文件夹1024", "我是保存下来的图片1024.png")val load = Glide.with(activity).asFile().load(urlImage).submit()val observable = Observable.just{}.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).map {load.get()}.observeOn(AndroidSchedulers.mainThread()).doOnError {val alarmToast = if (it is FileNotFoundException) "文件地址错误"else "图片加载失败"ToastUtils.showToast(alarmToast)}.subscribe {try {if (it.exists()) {if (it.copyTo(file).exists()) {ToastUtils.showToast("图片保存成功")}}} catch (e: Exception) {e.printStackTrace()}}}}, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
PS: 合适的地方(onDestroy)结束observable ,避免泄漏。
这篇关于Android Androidx Glide下载图片到指定文件夹 Glide版本4.+ Glide4.+的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!