【问题标题】:Loading Images With Picasso From The Internal Storage从内部存储加载 Picasso 图像
【发布时间】:2015-10-24 23:09:18
【问题描述】:

我将一些 JPEG 图像保存到手机存储中,我想将使用毕加索的图像加载到 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.setImageBitmap(getSavedImage(this,user.username+"_Profile"));

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

【问题讨论】:

标签: android image file bitmap picasso


【解决方案1】:

在 Kotlin 中使用最新版本的毕加索:

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

【讨论】:

    【解决方案2】:

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

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

    将图像加载到 ImageView 中。

    【讨论】:

    • 这里的关键是使用 .load(new File(path)) 而不是 .load(path)。像魅力一样工作。我正在使用它从内部存储加载图像
    猜你喜欢
    • 2018-06-27
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多