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

标签 android image file bitmap picasso

我将一些 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"));

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

最佳答案

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

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

将图像加载到 ImageView 中。

关于android - 使用 Picasso 从内部存储加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31777206/

相关文章:

Android如何在空查询中搜索

php - 如何为较大的图像生成多个缩略图?

html - div 高度与最小高度相同的背景图像

java - 将 JPG 转换为具有背景透明度的 PNG

c - 了解文件截断

python - Django 日志记录 : Cannot create logs per day

c - 将数组中的字符打印到 C 中的输出文件

android - 在 Android 和 IOS 中,我可以在不打开邮件应用程序的情况下使用 IONIC 发送邮件吗?

android - 如何根据日期和时间对我的 RecyclerView 进行排序

android - 垂直 LinearLayout 的分隔线?