From e9b8bebd1adcc1d5d6ceda5fdd42ba985d8f4c8d Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Fri, 5 Aug 2022 21:13:03 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E4=BC=98=E5=8C=96=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD=20&=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/activities/PhotoViewActivity.kt | 4 +- .../post/fragments/PhotoViewFragment.java | 2 +- .../post/fragments/PreferencesFragment.kt | 2 +- .../tieba/post/utils/GlideCacheUtil.java | 14 +-- .../tieba/post/utils/ImageUtil.java | 87 +++++++++---------- app/src/main/res/values/strings.xml | 4 +- app/src/main/res/xml/file_paths_share_img.xml | 6 +- app/src/main/res/xml/preferences.xml | 2 +- 8 files changed, 58 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/PhotoViewActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/PhotoViewActivity.kt index 6d47b0c1..659eaceb 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/PhotoViewActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/PhotoViewActivity.kt @@ -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) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PhotoViewFragment.java b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PhotoViewFragment.java index d0227f79..de98d908 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PhotoViewFragment.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PhotoViewFragment.java @@ -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; } }) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt index 39039710..871b1c20 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt @@ -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") diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/GlideCacheUtil.java b/app/src/main/java/com/huanchengfly/tieba/post/utils/GlideCacheUtil.java index d0acc4ec..ed1b337d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/GlideCacheUtil.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/GlideCacheUtil.java @@ -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(); diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/ImageUtil.java b/app/src/main/java/com/huanchengfly/tieba/post/utils/ImageUtil.java index 6c971c01..03adc2f3 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/ImageUtil.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/ImageUtil.java @@ -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; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5b1285e5..fa8b0b00 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -145,8 +145,8 @@ 取消点赞失败 %1$s 复制吧名 加载失败(%1$d) %2$s - 清除缓存 - 清除缓存成功 + 清除图片缓存 + 成功清除图片缓存 当前共有缓存 %1$s 切换成功 你还没有设置小尾巴 diff --git a/app/src/main/res/xml/file_paths_share_img.xml b/app/src/main/res/xml/file_paths_share_img.xml index 31ca5852..bf26f19d 100644 --- a/app/src/main/res/xml/file_paths_share_img.xml +++ b/app/src/main/res/xml/file_paths_share_img.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 998ba5e2..a77cb7be 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -273,7 +273,7 @@ + android:title="@string/title_clear_picture_cache" />