使用Glide共享缓存的图片会丢失扩展类型

时间:2020-01-22 14:55:25

标签: android image caching android-glide

我正在尝试使用Glide共享缓存的图像。我可以共享文件,但是它丢失了扩展名(mimeType),并且已作为扩展名为.0的二进制文件共享。

  glide
  .asBitmap()
  .format(DecodeFormat.PREFER_ARGB_8888)
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .load("url")

这是我从缓存中获取文件的方式

fun getFileFromCache(url: String) = glide.downloadOnly().load("url")

任何想法或建议,我该如何解决?

1 个答案:

答案 0 :(得分:0)

获得.0扩展名的原因是downloadOnly为您提供了来自SOURCE缓存的文件,并且DiskLruCache内置了版本控制功能。根据您对pickedPhotoPath的处理方式,我建议将其复制(不要重命名)到最终位置(您可以从photoUri获取内容类型或文件名),或者通过以下方式将其保存在内存中: 您可以将其复制(而不重命名)到最终位置(可以从photoUri获取内容类型或文件名),或将其保留在内存中。这是可以保存图像的代码(在Java中,但是我确定您可以将其转换为kotlin):

public static synchronized void saveImage(final Context context, final String imageUrl) {
    Glide.with(context.getApplicationContext())
         .load(imageUrl)
         .downloadOnly(new SimpleTarget<File>() {
             @Override public void onResourceReady(File src, GlideAnimation<? super File> glideAnimation) {
                 new GalleryFileSaver(context, imageUrl, src).execute();
             }
         });
}

private static class GalleryFileSaver extends AsyncTask<Void, Void, File> {
    private final Context context;
    private final String imageUrl;
    private final File src;
    public GalleryFileSaver(Context context, String imageUrl, File src) {
        this.context = context;
        this.imageUrl = imageUrl;
        this.src = src;
    }

    @Override protected File doInBackground(Void... params) {
        try {
            dst = new File(getGalleryFolder(), getTargetFileName());
            copy(new FileInputStream(src), new FileOutputStream(dst));
        } catch (IOException e) {
            dst = null;
            e.printStackTrace();
        }
        return dst;
    }
    @Override protected void onPostExecute(File dst) {
        String message = dst != null? "Saved file to " + dst : "Failed to save file from " + src;
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
    private void copy(InputStream in, OutputStream out) throws IOException {
        try {
            byte[] buf = new byte[1024 * 16];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            try { in.close(); } catch (IOException ignore) { }
            try { out.close(); } catch (IOException ignore) { }
        }
    }
    private String getTargetFileName() throws IOException {
        Random random = new Random();
        String ext = android.webkit.MimeTypeMap.getFileExtensionFromUrl(imageUrl);
        String name = String.valueOf(random.nextInt()) + ".jpg";
        Log.w("saeed", name);
        return name;
    }
    private File getGalleryFolder() throws IOException {
        File gallery = new File(APP_GALLERY_IMAGE_PATH);
        if (!gallery.mkdirs() && (!gallery.exists() || !gallery.isDirectory())) {
            throw new IOException("Invalid gallery path: " + gallery);
        }
        return gallery;
    }
}