从recyclerview保存更新的图像,并在离线Android时显示

问题描述 投票:0回答:1

我是Android的新手。在我的应用程序中,我有一个休息api,其中包含公司用户的信息。对于exmaple名称,电子邮件,电话等我也使用此信息实现图像。图像列表来自网址。我使用Glide图像库来显示图像。现在的问题是在我使用.diskCacheStrategy(DiskCacheStrategy.ALL)这一行将图像缓存到设备之前。它始终显示以前存储的图像。我也尝试过毕加索。但是当用户拍摄新照片时,图像不会更新。所以我写了下面的内容。

 .skipMemoryCache( true )
 .diskCacheStrategy( DiskCacheStrategy.NONE )

现在出现了一个新问题,当用户离线时,imageview上没有图像。我想保存更新的图像并显示到recyclelerview,即使用户离线时也是如此。我正在发表关于来自yester的堆栈溢出的很多文章。但没有找到任何合适的答案。

我通过以下方式在适配器中实现了Glide

Glide.with( holder.itemView.getContext() )
                    .load( Constants.HTTP.PHOTO_URL + currentColleague.getMail() )
                    .skipMemoryCache( true )
                    .diskCacheStrategy( DiskCacheStrategy.NONE )
                    .signature( new StringSignature( String.valueOf(Constants.HTTP.PHOTO_URL + currentColleague.getMail() ) ) )
                    .into( holder.colleaguePicture );

有没有办法从url保存图像并显示到recyclerview。

可能这不是相关的,但我有理解活动类

    public class MyColleaguesPage extends AppCompatActivity implements MyColleaguesAdapter.ColleagueListListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycolleagues_layout);


        if (getNetworkAvailability()) {
            rq = Volley.newRequestQueue(this);

            recyclerView = this.findViewById(R.id.colleagues_recycler);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this));
            recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

            myColleagueList = new ArrayList<>();

            sendRequest();

        } else {

            colleagueConfigViews();
        }


    public boolean getNetworkAvailability() {
        return Utils.isNetworkAvailable(getApplicationContext());
    }

    public void sendRequest() {

        trustAllCertificates();

        CookieHandler.setDefault( cookieManager );

        if (cookieManager==null) {

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else {
            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest( Request.Method.GET, UPLOAD_URL + "/api/users", null, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    for (int i = 0; i < response.length(); i++) {

                        MyColleagueModel mycolleague = new MyColleagueModel();

                        try {
                            JSONObject object = response.getJSONObject( i );

                            mycolleague.setName( object.optString( "name" ) );
                            mycolleague.setGivenName( object.optString( "givenName" ) );
                            mycolleague.setCompany( object.optString( "company" ) );
                            mycolleague.setTitle( object.optString( "title" ) );
                            mycolleague.setMail( object.optString( "mail" ) );
                            mycolleague.setMobile( object.optString( "mobile" ) );
                            mycolleague.setDepartment( object.optString( "department" ) );

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        myColleagueList.add( mycolleague );

                    }

                    adapter = new MyColleaguesAdapter( myColleagueList, MyColleaguesPage.this );

                    recyclerView.setAdapter( adapter );

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i( "Volley Error: ", error.toString() );
                }
            } );

            rq.add( jsonArrayRequest );
        }
    }

}

更新的答案

 Glide.with( holder.itemView.getContext() )
                .load( Constants.HTTP.PHOTO_URL + currentColleague.getMail() )
                .skipMemoryCache(getNetworkAvailability())

                .signature( new StringSignature( String.valueOf(Constants.HTTP.PHOTO_URL + currentColleague.getMail() ) ) )
                .into( holder.colleaguePicture );



 public boolean getNetworkAvailability() {
    return Utils.isNetworkAvailable(context);
}
android image android-recyclerview picasso android-glide
1个回答
0
投票

只是为了澄清问题:您的回收站视图显示上一段图像一段时间,然后在下载时使用新图像更新此图像,对吗?

如果这是错误,那么你可以通过告诉滑动在onBindViewHolder方法的图像视图中放置占位符来轻松修复它。

GlideApp.with(fragment)
  .load(url)
  .placeholder(R.drawable.placeholder) // <-- this should fix
  .into(view);

如果您没有占位符,则可以将空图像设置为imageView:

imageView.setImageDrawable(null); //empty imageView

这允许您恢复滑行的缓存行为。

为什么会这样?回收站视图将重复使用之前显示的行,从而创建您注意到的奇怪效果。如果您不告诉滑行立即显示其他东西作为占位符,则重用行的图像视图将显示上一个图像。

编辑

告诉glide使缓存资源无效的唯一方法是使用:

.signature()

提供您自己的方式来确定用户是否更新了图像。服务器应提供此类信息。也许在用户详细信息响应中,服务器可以添加lastModifiedPicture时间戳。鉴于您可以检测您的签名功能并告诉滑行下载新图像。

Example (from glide github):

Glide.with(yourFragment)
    .load(yourFileDataModel)
    .signature(new StringSignature("your signature string derived from the lastModifiedPicture timestamp"))
    .into(yourImageView);

是唯一的方法。

© www.soinside.com 2019 - 2024. All rights reserved.