使用 Picasso 从内部存储加载图像

Loading Images With Picasso From The Internal Storage

我将一些 JPEG 图像保存到手机存储中,我想使用 picasso 将其加载到 ImageView 中。不过我遇到了麻烦,无论我尝试什么,我都无法加载图像,ImageView 最终变成空白。

以下是我保存和检索图像的方法:

 private void saveImage(Context context, String name, Bitmap bitmap){
    name=name+".JPG";
    FileOutputStream out;
    try {
        out = context.openFileOutput(name, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private Bitmap getSavedImage(Context context, String name){
    name=name+".JPG";
    try{
        FileInputStream fis = context.openFileInput(name);
        Bitmap bitmap = BitmapFactory.decodeStream(fis);
        fis.close();
        return bitmap;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return null;
}

如您所见,返回的图像是位图,如何使用 picasso 将其加载到 imageview 中? 我知道我可以像这样将位图加载到图像中:

imageView.setImageBitmap(getSavedImage(this,user.username+"_Profile"));

但是我有一个 class,它使用 picasso 将图像(用户个人资料照片)四舍五入,所以我需要用 picasso 加载它。

首先获取要加载图片的路径。然后,您可以使用

Picasso.with(context).load(new File(path)).into(imageView);

将图像加载到 ImageView。

在 Kotlin 中使用最新版本的 Picasso:

       Picasso.get()
            .load(File(context.filesDir, filename))
            .into(imageView)