Images from google api are getting cached and I need to figure out how to remove them when the app closes

梦想的初衷 提交于 2019-12-11 17:25:35

问题


I built a recyclerview that gets data from Google places api and displays it. I didn't notice that the photos are being cached every time I open the app. According to google, that is illegal so I need to get rid of caching.

I initially thought it was because i used the Picasso library but i added code for that, and now I'm stuck. I get the data from google in a bitmap then send it through to the recyclerview. I pass it through a URI creator to be able to use it with the Picasso Library and I think that's where the photos get cached.

The RecyclerView Adapter

private Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

@Override
public void onBindViewHolder(@NonNull VenueViewHolder venueViewHolder, int i) {
    Collections.sort(vIAL, (o1, o2) -> o1.getVenueName().compareToIgnoreCase(o2.getVenueName()));
    VenueItem currentItem = vIAL.get(i);

    Picasso.with(context)
            .load(getImageUri(context, currentItem.getVenueImage()))
            .fit()
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .into(venueViewHolder.vIV);
    venueViewHolder.vTV.setText(currentItem.getVenueName());
    Log.i(TAG, "The photo should have been on screen");
}

The activity that I use to get info from Google

 private void getClubs() {
    for (String club : clubs) {
        FetchPlaceRequest request = FetchPlaceRequest.newInstance(club, placeFields);
        placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
            Place place = response.getPlace();

            PhotoMetadata photoMetadata = Objects.requireNonNull(place.getPhotoMetadatas()).get(0);
            //String attributions = photoMetadata.getAttributions();

            FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata).setMaxHeight(1600).setMaxWidth(1600).build();
            placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {

                Bitmap bitmap = fetchPhotoResponse.getBitmap();

                vIL.add(new VenueItem( /*Photo*/bitmap, place.getName()));

                vRV.setLayoutManager(vLM);
                vRV.setAdapter(vAdapter);
            }).addOnFailureListener((exception) -> {
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    int statusCode = apiException.getStatusCode();
                    // Handle error with given status code.
                    Log.e(TAG, "Place not found: " + exception.getMessage());
                }
            });

            Log.i(TAG, "Place found: " + place.getName());
        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });
    }
}

My question is - how do I remove the cached photos from the phone when the app closes so it's not illegal, or how do I prevent them from being cached in the first place? Also, if possible - I used Picasso so that I could manipulate the images as they are displayed to the recyclerview and this is the only way I know how - is there a better way to present the pictures uniformally?

EDIT: I am assuming that the getImageUri method is the culprit since adding code to the Picasso code does nothing.


回答1:


Picasso automatically caches the loaded images, So that next time they will be loaded from the cache.

In order to skip image caching use this :

Picasso.with(context).load(imageUrl)
            .error(R.drawable.error)
            .placeholder(R.drawable.placeholder)
            .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) // prevents caching
            .into(imageView);

Hope this helps!



来源:https://stackoverflow.com/questions/57245472/images-from-google-api-are-getting-cached-and-i-need-to-figure-out-how-to-remove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!