Glide 不从文件路径加载图像

Glide not loading Image from File path

我在使用 glide 加载图像时遇到问题。当我尝试使用 Android 默认值 setImageBitmap(bitmap); 加载图像时,我试图在从图库中选取图像后加载图像 它有效,但我遇到了内存泄漏问题。

然后我尝试使用 Glide 图像加载器来避免内存泄漏问题。但是我无法使用文件路径加载图像。

   Glide.with(_A)
                        .load(new File(uri.getPath()))
                        .asBitmap()
                        .override(w, w)
                        .centerCrop()
                        .placeholder(R.drawable.diploma)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

我也试过加载 Uri,但也没用。

然后我尝试从 URL Glide 加载 URL 图像加载图像。效果很好。

  Glide
                        .with(_A)
                        .load("https://yt3.ggpht.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAAAAAA/OixOH_h84Po/s900-c-k-no-mo-rj-c0xffffff/photo.jpg")
                        .asBitmap()
                        .override(w, w)
                        .centerCrop()
                        .placeholder(R.drawable.diploma)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

然后我尝试使用默认函数从 Uri 加载图像 imageView.setImageURI(uri); 此功能有效 图片已加载。

Uri 没有问题,那为什么我无法加载图片?

谁能帮我找到解决这个问题的方法?

正如所讨论的,这里是将图像保存在特定路径以及如何检索它的代码。另外,我正在使用 Realm 来保存图像的路径,您可以使用其他任何东西。这是保存图片的代码:

private void saveToInternalStorage(Bitmap bitmapImage, final String fileName) {
    ContextWrapper cw = new ContextWrapper(getContext());
    directory = cw.getDir("myloImages", Context.MODE_PRIVATE);
    if (!directory.exists()) {
        directory.mkdir();
    }
    mypath = new File(directory, fileName + ".png");

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
        Log.v("IMAGE PATH", mypath.getAbsolutePath());
    } catch (Exception e) {
        Log.e("SAVE_IMAGE", e.getMessage(), e);
    }

    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            PhotosRealm photosRealm = new PhotosRealm();
            photosRealm.setImageLocation(directory.getAbsolutePath() + "/" + fileName + ".png");
            realm.copyToRealm(photosRealm);
            // I'm using realm to save location.
        }
    });
    finish();
}

现在从该位置检索图像。它与您的代码相同:

private RealmResults<PhotosRealm> list;

Glide.with(context).load(list.get(0).getImageLocation()).asBitmap().into(imageView);