ImageDownloadThread.java
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
}
}