ImageDownloadThread.java 5.3 KB
package com.wd.common.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.wd.foundation.wdkit.file.MyFileUtils;
import com.wd.foundation.wdkit.image.GlideApp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by v-junjlu on 2018/3/20.
 */
public class ImageDownloadThread extends Thread {
    private String mUrl;
    private Uri mUri;
    private boolean isUrl;
    private Context mContext;
    private ImageDownLoadCallBack mCallBack;
    public interface ImageDownLoadCallBack {
        void onDownLoadSuccess();

        void onDownLoadFailed();
    }

    public ImageDownloadThread(Context context, String url, ImageDownLoadCallBack callBack) {
        this.mUrl = url;
        isUrl = true;
        this.mCallBack = callBack;
        this.mContext = context;
    }

    public ImageDownloadThread(Context context, Uri uri, ImageDownLoadCallBack callBack) {
        this.mUri = uri;
        isUrl = false;
        this.mCallBack = callBack;
        this.mContext = context;
    }

    @Override
    public void run() {
        boolean isSuccess = false;
        try {
            String url = isUrl ? mUrl : mUri.getPath();
            // 使用glide下载图片
            File orgFile = GlideApp.with(mContext).download(url).submit().get();

            boolean isGif = TextUtils.isEmpty(url) ? false : url.toLowerCase().endsWith(".gif");
            String fileName;
            if (isGif) {
                fileName = SystemClock.uptimeMillis() + ".gif";
            } else {
                fileName = SystemClock.uptimeMillis() + ".jpg";
            }
            // 通知图库更新
            isSuccess = insertImage(mContext, fileName, orgFile);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                // 通知图库更新
                mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(orgFile)));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (isSuccess) {
                mCallBack.onDownLoadSuccess();
            } else {
                mCallBack.onDownLoadFailed();
            }
        }
    }

    /**
     * 获取照片的mine_type
     *
     * @param path
     * @return
     */
    private String getPhotoMimeType(String path){
        String lowerPath = path.toLowerCase();
        if (lowerPath.endsWith("jpg") || lowerPath.endsWith("jpeg")) {
            return "image/jpeg";
        } else if (lowerPath.endsWith("png")) {
            return "image/png";
        } else if (lowerPath.endsWith("gif")) {
            return "image/gif";
        }
        return "image/jpeg";
    }

    private boolean insertImage(Context context, String fileName,File photoFile) {
        Uri imageCollection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            imageCollection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        } else {
            imageCollection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getPhotoMimeType(fileName));
        // 判断是否android10 以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // 设置独占锁:耗时操作,独占访问权限,完成操作需复位
            contentValues.put(MediaStore.Images.Media.IS_PENDING, 1);
        }
        ContentResolver resolver = context.getApplicationContext().getContentResolver();
        Uri insertUri = resolver.insert(imageCollection,contentValues);
        if (insertUri!=null){
            FileInputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                outputStream = resolver.openOutputStream(insertUri);
                inputStream = new FileInputStream(photoFile);
                MyFileUtils.copyFile(inputStream,outputStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(inputStream != null){
                    try{
                        inputStream.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
                if(outputStream != null){
                    try{
                        outputStream.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
            //判断是否android10 以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                // 复位(解除)独占权限
                contentValues.clear();
                contentValues.put(MediaStore.Images.Media.IS_PENDING, 0);
                resolver.update(insertUri, contentValues, null, null);
            }
            return true;
        }
        return false;
    }
}