pref: 优化分享图片下载 & 缓存清除

This commit is contained in:
HuanCheng65 2022-08-05 21:13:03 +08:00
parent a071fd1f5d
commit e9b8bebd1a
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
8 changed files with 58 additions and 63 deletions

View File

@ -269,8 +269,7 @@ class PhotoViewActivity : BaseActivity(), OnChangeBottomBarVisibilityListener,
R.id.menu_save_image -> {
ImageUtil.download(
this,
mAdapter.getBean(mViewPager.currentItem).originUrl,
mAdapter.getBean(mViewPager.currentItem).isGif
mAdapter.getBean(mViewPager.currentItem).originUrl
)
return true
}
@ -279,7 +278,6 @@ class PhotoViewActivity : BaseActivity(), OnChangeBottomBarVisibilityListener,
ImageUtil.download(
this,
mAdapter.getBean(mViewPager.currentItem).originUrl,
mAdapter.getBean(mViewPager.currentItem).isGif,
true
) { uri: Uri? ->
val intent = Intent(Intent.ACTION_SEND)

View File

@ -177,7 +177,7 @@ public class PhotoViewFragment extends BaseFragment {
.setItems(strArray, (DialogInterface dialog, int which) -> {
switch (which) {
case 0:
ImageUtil.download(getAttachContext(), photoViewBean.getOriginUrl(), photoViewBean.isGif());
ImageUtil.download(getAttachContext(), photoViewBean.getOriginUrl());
break;
}
})

View File

@ -211,7 +211,7 @@ class PreferencesFragment : PreferencesFragment() {
GlideCacheUtil.getInstance().clearImageAllCache(attachContext)
if (view != null) Util.createSnackbar(
requireView(),
R.string.toast_clear_cache_success,
R.string.toast_clear_picture_cache_success,
Snackbar.LENGTH_SHORT
).show()
preference.summary = attachContext.getString(R.string.tip_cache, "0.0B")

View File

@ -5,7 +5,7 @@ import android.os.Looper;
import android.text.TextUtils;
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 java.io.File;
@ -95,8 +95,9 @@ public class GlideCacheUtil {
public void clearImageAllCache(Context context) {
clearImageDiskCache(context);
clearImageMemoryCache(context);
String ImageExternalCatchDir = context.getExternalCacheDir() + ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
deleteFolderFile(ImageExternalCatchDir, true);
String imageExternalCacheDir = context.getExternalCacheDir() + File.separator + ExternalPreferredCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
deleteFolderFile(imageExternalCacheDir, false);
deleteFolderFile(context.getCacheDir() + File.separator + ".shareTemp", false);
}
/**
@ -106,7 +107,9 @@ public class GlideCacheUtil {
*/
public String getCacheSize(Context context) {
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) {
e.printStackTrace();
}
@ -118,9 +121,8 @@ public class GlideCacheUtil {
*
* @param file file
* @return size
* @throws Exception
*/
private long getFolderSize(File file) throws Exception {
private long getFolderSize(File file) {
long size = 0;
try {
File[] fileList = file.listFiles();

View File

@ -242,14 +242,42 @@ public class ImageUtil {
}
@SuppressLint("StaticFieldLeak")
public static void download(Context context, String url, boolean gif) {
download(context, url, gif, false, null);
public static void download(Context context, String url) {
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")
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) {
downloadAboveQ(context, url, forShare, taskCallback);
downloadAboveQ(context, url);
return;
}
PermissionUtils.INSTANCE.askPermission(
@ -260,12 +288,12 @@ public class ImageUtil {
),
R.string.toast_no_permission_save_photo,
() -> {
downloadBelowQ(context, url, forShare, taskCallback);
downloadBelowQ(context, url);
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) {
return;
}
@ -278,9 +306,6 @@ public class ImageUtil {
}
Log.i(TAG, "download: fileName = " + fileName);
String relativePath = Environment.DIRECTORY_PICTURES + File.separator + FILE_FOLDER;
if (forShare) {
relativePath += File.separator + "shareTemp";
}
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.RELATIVE_PATH, relativePath);
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
@ -304,37 +329,16 @@ public class ImageUtil {
}
return;
}
if (!forShare)
Toast.makeText(context, context.getString(R.string.toast_photo_saved, relativePath), Toast.LENGTH_SHORT).show();
else if (taskCallback != null)
taskCallback.onGetUri(uri);
Toast.makeText(context, context.getString(R.string.toast_photo_saved, relativePath), Toast.LENGTH_SHORT).show();
}).execute();
}
private static void downloadAboveQ(Context context, String url) {
downloadAboveQ(context, url, false, null);
}
private static void downloadBelowQ(Context context, String url, boolean forShare, @Nullable ShareTaskCallback taskCallback) {
private static void downloadBelowQ(Context context, String url) {
new DownloadAsyncTask(context, url, file -> {
File pictureFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
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 (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());
if (isGifFile(file)) {
fileName = changeFileExtension(fileName, ".gif");
@ -345,12 +349,8 @@ public class ImageUtil {
}
copyFile(file, destFile);
checkGifFile(destFile);
if (!forShare) {
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();
} else if (taskCallback != null) {
taskCallback.onGetUri(FileProvider.getUriForFile(context, context.getPackageName() + ".share.FileProvider", destFile));
}
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();
}
}).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) {
String fileName = URLUtil.guessFileName(picUrl, null, MimeType.JPEG.toString());
return fileName.replace(".jpg", "");
@ -394,7 +389,7 @@ public class ImageUtil {
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.menu_save_image:
download(view.getContext(), photoViewBeans.get(position).getOriginUrl(), false);
download(view.getContext(), photoViewBeans.get(position).getOriginUrl());
return true;
}
return false;
@ -422,7 +417,7 @@ public class ImageUtil {
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.menu_save_image:
download(view.getContext(), photoViewBeans.get(position).getOriginUrl(), false);
download(view.getContext(), photoViewBeans.get(position).getOriginUrl());
return true;
}
return false;

View File

@ -145,8 +145,8 @@
<string name="toast_unagree_failed">取消点赞失败 %1$s</string>
<string name="title_copy_forum_name">复制吧名</string>
<string name="toast_error">加载失败(%1$d) %2$s</string>
<string name="title_clear_cache">清除缓存</string>
<string name="toast_clear_cache_success">清除缓存成功</string>
<string name="title_clear_picture_cache">清除图片缓存</string>
<string name="toast_clear_picture_cache_success">成功清除图片缓存</string>
<string name="tip_cache">当前共有缓存 %1$s</string>
<string name="toast_switch_success">切换成功</string>
<string name="tip_no_little_tail">你还没有设置小尾巴</string>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resource>
<external-path
name="images"
path="Pictures/TiebaLite/shareTemp" />
<cache-path
name="shareCache"
path=".shareTemp" />
</resource>

View File

@ -273,7 +273,7 @@
<Preference
android:icon="@drawable/ic_round_offline_bolt_green"
android:key="clear_cache"
android:title="@string/title_clear_cache" />
android:title="@string/title_clear_picture_cache" />
<Preference
android:icon="@drawable/ic_round_info"