簡體   English   中英

android應用關閉后從設備中刪除照片

[英]Remove photos from device after android app closes

編輯:我前一天問了一個類似的問題,我根據自該問題以來收集的信息( 第一個緩存問題)撰寫了這篇文章。 我知道它們是相似的,但是此問題更為簡潔,以避免產生額外的信息。 我也不想刪除該帖子,因為它已經被回答了,即使答案還不夠。

我使用recyclerview來顯示從google places api獲得的照片。 我知道我不允許在運行時之外合法地緩存照片。 我不知道那是怎么回事,直到我隨機打開我的手機圖庫來查看我從Google獲得的照片的很多副本。

我的第一個猜測是問題是我使用Picasso ,所以我添加了代碼來修復它.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) .networkPolicy(NetworkPolicy.NO_CACHE)StackOverflow中找到它們很簡單,但這沒有實現。除了現在似乎只能下載一次,特別是在刪除它們時,無法修復任何內容。 我相信我消除了可能的問題,並概述了這個問題中的最后一個問題。

 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, MemoryPolicy.NO_STORE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .into(venueViewHolder.vIV);
    venueViewHolder.vTV.setText(currentItem.getVenueName());
    Log.i(TAG, "The photo should have been on screen");
}

我發現URI方法getImageUri是我遇到的另一個問題的答案,我需要一個URI來實現Picasso庫,以便可以在顯示照片之前對其進行操作。

我的問題是-關閉應用程序后如何刪​​除照片?

更新:我改變了策略以查看會發生什么並使用了Glide

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

     Glide.with(context)
            .load(currentItem.getVenueImage())
            .into(venueViewHolder.vIV);
    venueViewHolder.vTV.setText(currentItem.getVenueName());
}

它給出了致命的錯誤

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 4344032)

這是錯誤之一,在我第一次運行這個新代碼時並沒有發生,但是在第二次和第三次運行它時情況變得更糟。

我根據從@Padmini S早早得到的答案轉移了代碼,但他們在load部分使用了網址,並傳遞了位圖,因為我一生都無法弄清楚如何從Google Places API獲取URL而不是它們在Google Place Photos中提供的代碼。

我對編碼還比較陌生,因此這是我嘗試將需要學習的知識組合在一起的方法。 我對要搜索的內容一無所知,因此我在此根據從答案中收集到的新信息詢問是否可以替換

MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);

在我的代碼中,這樣我的照片就不會保存到手機的照片中?

最終更新我最終重寫了很多與此相關的代碼,並刪除了這些代碼。 我最終制作了一個arraylist類,以在runtime期間保存數組列表,這讓我刪除了出於無知而寫的大多數多余代碼。

我真的不知道它是否可以解決您的問題,但是您將圖像存儲在此處的設備本地存儲中,

private Uri getImageUri(Context inContext, Bitmap inImage) {

    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);

}

我從未使用過Google Places api,但通常api會為您返回一個圖像URL,您可以將該URL存儲在POJO類中,並像在下面的代碼中那樣直接在recyclerview行圖像視圖中顯示它,

public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.description.setText(links.get(i).getTitle());
        Glide.with(context).load(links.get(i).getImage_url()).into(viewHolder.image);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM