26.Android 下载图片保存到相册

26.Android 下载图片保存到相册


前言

有遇到没有这样的一种需求:浏览的大图后,点击保存下载高清原图到相册的需求。

现在的图片缓存大多都是Universal-Imager-Loader为多。但是我们在公司的某些离谱的需求(圈子系的需求,要求每条动态展示的图片不止6张,有一条30多张,直接报了Universal-Imager-Loader的OOM,怎么改配置都不能解决)上,后来调研了GlideFresco,发现Glide基本是完虐的节奏。

这里给大家提一个Glide小问题:Glide自带能把图片加载成圆角,Glide加载本地图片不能实现圆角。


实现思路

  • 1.自定义一个AsyncTask下载图片
  • 2.自定义一个Dialog显示下载进度
  • 3.自定义一个Handler刷新Dialog的进度
  • 4.下载完成后,保存图片到相册里

自定义Dialog

dialog_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="10dip" />

    <TextView
        android:id="@+id/load_info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="正在提交..."
        android:textColor="#000000" />

</LinearLayout>

CustomProgressBarDialog

    /**
     * 自定义进度条Dialog
     */
    public class CustomProgressBarDialog extends Dialog {
   

        private LayoutInflater mInflater;
        private Context mContext;
        private WindowManager.LayoutParams params;
        private View mView;
        private TextView promptTV;

        public CustomProgressBarDialog(Context context) {
            super(context);
            this.init(context);
        }

        public CustomProgressBarDialog(Context context, int themeResId) {
            super(context, themeResId);
            this.init(context);
        }

        protected CustomProgressBarDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            this.init(context);
        }

        private void init(Context context) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.mContext = context;
            this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mView = this.mInflater.inflate(R.layout.dialog_progressbar, null);
            setContentView(this.mView);

            // 设置window属性
            this.params = getWindow().getAttributes();
            this.params.gravity = Gravity.CENTER;
            // 去背景遮盖
            this.params.dimAmount = 0;
            this.params.alpha = 1.0f;
            // 不能关掉
            this.setCancelable(false);
            this.getWindow().setAttributes(this.params);
            this.promptTV = (TextView) findViewById(R.id.load_info_text);
        }

        /**
         * 设置内容
         *
         * @param prompt
         */
        public void setLoadPrompt(String prompt) {
            this.promptTV.setText(prompt);
        }

    }

自定义Handler

注意:这里可以参考Handler 通用模板

    private static final int HANDLER_LOADING = 262;

    /**
     * 刷新Dialog显示的进度Handler
     */
    private static class LoadingHandler extends Handler {
   
        private final WeakReference<DownloadImageToGalleryActivity> mActivity;

        public LoadingHandler(DownloadImageToGalleryActivity activity) {
        
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值