pref: 优化分享图片下载 & 缓存清除
This commit is contained in:
parent
a071fd1f5d
commit
e9b8bebd1a
|
|
@ -269,8 +269,7 @@ class PhotoViewActivity : BaseActivity(), OnChangeBottomBarVisibilityListener,
|
||||||
R.id.menu_save_image -> {
|
R.id.menu_save_image -> {
|
||||||
ImageUtil.download(
|
ImageUtil.download(
|
||||||
this,
|
this,
|
||||||
mAdapter.getBean(mViewPager.currentItem).originUrl,
|
mAdapter.getBean(mViewPager.currentItem).originUrl
|
||||||
mAdapter.getBean(mViewPager.currentItem).isGif
|
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -279,7 +278,6 @@ class PhotoViewActivity : BaseActivity(), OnChangeBottomBarVisibilityListener,
|
||||||
ImageUtil.download(
|
ImageUtil.download(
|
||||||
this,
|
this,
|
||||||
mAdapter.getBean(mViewPager.currentItem).originUrl,
|
mAdapter.getBean(mViewPager.currentItem).originUrl,
|
||||||
mAdapter.getBean(mViewPager.currentItem).isGif,
|
|
||||||
true
|
true
|
||||||
) { uri: Uri? ->
|
) { uri: Uri? ->
|
||||||
val intent = Intent(Intent.ACTION_SEND)
|
val intent = Intent(Intent.ACTION_SEND)
|
||||||
|
|
|
||||||
|
|
@ -177,7 +177,7 @@ public class PhotoViewFragment extends BaseFragment {
|
||||||
.setItems(strArray, (DialogInterface dialog, int which) -> {
|
.setItems(strArray, (DialogInterface dialog, int which) -> {
|
||||||
switch (which) {
|
switch (which) {
|
||||||
case 0:
|
case 0:
|
||||||
ImageUtil.download(getAttachContext(), photoViewBean.getOriginUrl(), photoViewBean.isGif());
|
ImageUtil.download(getAttachContext(), photoViewBean.getOriginUrl());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -211,7 +211,7 @@ class PreferencesFragment : PreferencesFragment() {
|
||||||
GlideCacheUtil.getInstance().clearImageAllCache(attachContext)
|
GlideCacheUtil.getInstance().clearImageAllCache(attachContext)
|
||||||
if (view != null) Util.createSnackbar(
|
if (view != null) Util.createSnackbar(
|
||||||
requireView(),
|
requireView(),
|
||||||
R.string.toast_clear_cache_success,
|
R.string.toast_clear_picture_cache_success,
|
||||||
Snackbar.LENGTH_SHORT
|
Snackbar.LENGTH_SHORT
|
||||||
).show()
|
).show()
|
||||||
preference.summary = attachContext.getString(R.string.tip_cache, "0.0B")
|
preference.summary = attachContext.getString(R.string.tip_cache, "0.0B")
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import android.os.Looper;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
|
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
|
||||||
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
|
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -95,8 +95,9 @@ public class GlideCacheUtil {
|
||||||
public void clearImageAllCache(Context context) {
|
public void clearImageAllCache(Context context) {
|
||||||
clearImageDiskCache(context);
|
clearImageDiskCache(context);
|
||||||
clearImageMemoryCache(context);
|
clearImageMemoryCache(context);
|
||||||
String ImageExternalCatchDir = context.getExternalCacheDir() + ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
|
String imageExternalCacheDir = context.getExternalCacheDir() + File.separator + ExternalPreferredCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
|
||||||
deleteFolderFile(ImageExternalCatchDir, true);
|
deleteFolderFile(imageExternalCacheDir, false);
|
||||||
|
deleteFolderFile(context.getCacheDir() + File.separator + ".shareTemp", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -106,7 +107,9 @@ public class GlideCacheUtil {
|
||||||
*/
|
*/
|
||||||
public String getCacheSize(Context context) {
|
public String getCacheSize(Context context) {
|
||||||
try {
|
try {
|
||||||
return getFormatSize(getFolderSize(new File(context.getCacheDir() + "/" + InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR)));
|
double glideCacheSize = getFolderSize(new File(context.getCacheDir(), InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
|
||||||
|
double shareCacheSize = getFolderSize(new File(context.getCacheDir(), ".shareTemp"));
|
||||||
|
return getFormatSize(glideCacheSize + shareCacheSize);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -118,9 +121,8 @@ public class GlideCacheUtil {
|
||||||
*
|
*
|
||||||
* @param file file
|
* @param file file
|
||||||
* @return size
|
* @return size
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
private long getFolderSize(File file) throws Exception {
|
private long getFolderSize(File file) {
|
||||||
long size = 0;
|
long size = 0;
|
||||||
try {
|
try {
|
||||||
File[] fileList = file.listFiles();
|
File[] fileList = file.listFiles();
|
||||||
|
|
|
||||||
|
|
@ -242,14 +242,42 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("StaticFieldLeak")
|
@SuppressLint("StaticFieldLeak")
|
||||||
public static void download(Context context, String url, boolean gif) {
|
public static void download(Context context, String url) {
|
||||||
download(context, url, gif, false, null);
|
download(context, url, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void downloadForShare(Context context, String url, @NonNull ShareTaskCallback taskCallback) {
|
||||||
|
new DownloadAsyncTask(context, url, file -> {
|
||||||
|
File pictureFolder = new File(context.getCacheDir(), ".shareTemp");
|
||||||
|
if (pictureFolder.exists() || pictureFolder.mkdirs()) {
|
||||||
|
String fileName = "share_" + System.currentTimeMillis();
|
||||||
|
File destFile = new File(pictureFolder, fileName);
|
||||||
|
if (!destFile.exists()) {
|
||||||
|
copyFile(file, destFile);
|
||||||
|
}
|
||||||
|
Uri shareUri;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
shareUri = FileProvider.getUriForFile(
|
||||||
|
context,
|
||||||
|
context.getPackageName() + ".share.FileProvider",
|
||||||
|
destFile
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
shareUri = Uri.fromFile(destFile);
|
||||||
|
}
|
||||||
|
taskCallback.onGetUri(shareUri);
|
||||||
|
}
|
||||||
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("StaticFieldLeak")
|
@SuppressLint("StaticFieldLeak")
|
||||||
public static void download(Context context, String url, boolean gif, boolean forShare, @Nullable ShareTaskCallback taskCallback) {
|
public static void download(Context context, String url, boolean forShare, @Nullable ShareTaskCallback taskCallback) {
|
||||||
|
if (forShare) {
|
||||||
|
if (taskCallback != null) downloadForShare(context, url, taskCallback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
downloadAboveQ(context, url, forShare, taskCallback);
|
downloadAboveQ(context, url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PermissionUtils.INSTANCE.askPermission(
|
PermissionUtils.INSTANCE.askPermission(
|
||||||
|
|
@ -260,12 +288,12 @@ public class ImageUtil {
|
||||||
),
|
),
|
||||||
R.string.toast_no_permission_save_photo,
|
R.string.toast_no_permission_save_photo,
|
||||||
() -> {
|
() -> {
|
||||||
downloadBelowQ(context, url, forShare, taskCallback);
|
downloadBelowQ(context, url);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void downloadAboveQ(Context context, String url, boolean forShare, @Nullable ShareTaskCallback taskCallback) {
|
private static void downloadAboveQ(Context context, String url) {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -278,9 +306,6 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
Log.i(TAG, "download: fileName = " + fileName);
|
Log.i(TAG, "download: fileName = " + fileName);
|
||||||
String relativePath = Environment.DIRECTORY_PICTURES + File.separator + FILE_FOLDER;
|
String relativePath = Environment.DIRECTORY_PICTURES + File.separator + FILE_FOLDER;
|
||||||
if (forShare) {
|
|
||||||
relativePath += File.separator + "shareTemp";
|
|
||||||
}
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(MediaStore.Images.Media.RELATIVE_PATH, relativePath);
|
values.put(MediaStore.Images.Media.RELATIVE_PATH, relativePath);
|
||||||
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
|
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
|
||||||
|
|
@ -304,37 +329,16 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!forShare)
|
|
||||||
Toast.makeText(context, context.getString(R.string.toast_photo_saved, relativePath), Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, context.getString(R.string.toast_photo_saved, relativePath), Toast.LENGTH_SHORT).show();
|
||||||
else if (taskCallback != null)
|
|
||||||
taskCallback.onGetUri(uri);
|
|
||||||
}).execute();
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void downloadAboveQ(Context context, String url) {
|
private static void downloadBelowQ(Context context, String url) {
|
||||||
downloadAboveQ(context, url, false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void downloadBelowQ(Context context, String url, boolean forShare, @Nullable ShareTaskCallback taskCallback) {
|
|
||||||
new DownloadAsyncTask(context, url, file -> {
|
new DownloadAsyncTask(context, url, file -> {
|
||||||
File pictureFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
|
File pictureFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
|
||||||
File appDir;
|
File appDir;
|
||||||
if (forShare) {
|
|
||||||
appDir = new File(pictureFolder, FILE_FOLDER + File.separator + "shareTemp");
|
|
||||||
} else {
|
|
||||||
appDir = new File(pictureFolder, FILE_FOLDER);
|
appDir = new File(pictureFolder, FILE_FOLDER);
|
||||||
}
|
|
||||||
if (appDir.exists() || appDir.mkdirs()) {
|
if (appDir.exists() || appDir.mkdirs()) {
|
||||||
if (forShare) {
|
|
||||||
File nomedia = new File(appDir, ".nomedia");
|
|
||||||
if (!nomedia.exists()) {
|
|
||||||
try {
|
|
||||||
nomedia.createNewFile();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
||||||
if (isGifFile(file)) {
|
if (isGifFile(file)) {
|
||||||
fileName = changeFileExtension(fileName, ".gif");
|
fileName = changeFileExtension(fileName, ".gif");
|
||||||
|
|
@ -345,12 +349,8 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
copyFile(file, destFile);
|
copyFile(file, destFile);
|
||||||
checkGifFile(destFile);
|
checkGifFile(destFile);
|
||||||
if (!forShare) {
|
|
||||||
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destFile.getPath()))));
|
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destFile.getPath()))));
|
||||||
Toast.makeText(context, context.getString(R.string.toast_photo_saved, destFile.getPath()), Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, context.getString(R.string.toast_photo_saved, destFile.getPath()), Toast.LENGTH_SHORT).show();
|
||||||
} else if (taskCallback != null) {
|
|
||||||
taskCallback.onGetUri(FileProvider.getUriForFile(context, context.getPackageName() + ".share.FileProvider", destFile));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).execute();
|
}).execute();
|
||||||
}
|
}
|
||||||
|
|
@ -366,11 +366,6 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("StaticFieldLeak")
|
|
||||||
private static void downloadBelowQ(Context context, String url) {
|
|
||||||
downloadBelowQ(context, url, false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getPicId(String picUrl) {
|
public static String getPicId(String picUrl) {
|
||||||
String fileName = URLUtil.guessFileName(picUrl, null, MimeType.JPEG.toString());
|
String fileName = URLUtil.guessFileName(picUrl, null, MimeType.JPEG.toString());
|
||||||
return fileName.replace(".jpg", "");
|
return fileName.replace(".jpg", "");
|
||||||
|
|
@ -394,7 +389,7 @@ public class ImageUtil {
|
||||||
popupMenu.setOnMenuItemClickListener(item -> {
|
popupMenu.setOnMenuItemClickListener(item -> {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menu_save_image:
|
case R.id.menu_save_image:
|
||||||
download(view.getContext(), photoViewBeans.get(position).getOriginUrl(), false);
|
download(view.getContext(), photoViewBeans.get(position).getOriginUrl());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -422,7 +417,7 @@ public class ImageUtil {
|
||||||
popupMenu.setOnMenuItemClickListener(item -> {
|
popupMenu.setOnMenuItemClickListener(item -> {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menu_save_image:
|
case R.id.menu_save_image:
|
||||||
download(view.getContext(), photoViewBeans.get(position).getOriginUrl(), false);
|
download(view.getContext(), photoViewBeans.get(position).getOriginUrl());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -145,8 +145,8 @@
|
||||||
<string name="toast_unagree_failed">取消点赞失败 %1$s</string>
|
<string name="toast_unagree_failed">取消点赞失败 %1$s</string>
|
||||||
<string name="title_copy_forum_name">复制吧名</string>
|
<string name="title_copy_forum_name">复制吧名</string>
|
||||||
<string name="toast_error">加载失败(%1$d) %2$s</string>
|
<string name="toast_error">加载失败(%1$d) %2$s</string>
|
||||||
<string name="title_clear_cache">清除缓存</string>
|
<string name="title_clear_picture_cache">清除图片缓存</string>
|
||||||
<string name="toast_clear_cache_success">清除缓存成功</string>
|
<string name="toast_clear_picture_cache_success">成功清除图片缓存</string>
|
||||||
<string name="tip_cache">当前共有缓存 %1$s</string>
|
<string name="tip_cache">当前共有缓存 %1$s</string>
|
||||||
<string name="toast_switch_success">切换成功</string>
|
<string name="toast_switch_success">切换成功</string>
|
||||||
<string name="tip_no_little_tail">你还没有设置小尾巴</string>
|
<string name="tip_no_little_tail">你还没有设置小尾巴</string>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resource>
|
<resource>
|
||||||
<external-path
|
<cache-path
|
||||||
name="images"
|
name="shareCache"
|
||||||
path="Pictures/TiebaLite/shareTemp" />
|
path=".shareTemp" />
|
||||||
</resource>
|
</resource>
|
||||||
|
|
@ -273,7 +273,7 @@
|
||||||
<Preference
|
<Preference
|
||||||
android:icon="@drawable/ic_round_offline_bolt_green"
|
android:icon="@drawable/ic_round_offline_bolt_green"
|
||||||
android:key="clear_cache"
|
android:key="clear_cache"
|
||||||
android:title="@string/title_clear_cache" />
|
android:title="@string/title_clear_picture_cache" />
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:icon="@drawable/ic_round_info"
|
android:icon="@drawable/ic_round_info"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue