2017-01-10 48 views
1

我需要將設置到圖像視圖的圖像保存到內存中,這樣當用戶脫機時,我可以直接從內存中使用這些圖像。 到目前爲止,我已嘗試使用Target,但我在將位圖設置爲imageview時出現錯誤。 有沒有辦法做到這一點,但不影響性能?將圖像從畢加索保存到內存

private static Target getTarget(final String url, final ImageView thumbIV){ 
    Target target = new Target() { 
     @Override 
     public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom loadedFrom) { 
      new Thread(new Runnable() { 

       @Override 
       public void run() { 
        thumbIV.setImageBitmap(bitmap); 
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url); 
        try { 
         file.createNewFile(); 
         FileOutputStream ostream = new FileOutputStream(file); 
         bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream); 
         ostream.flush(); 
         ostream.close(); 
        } catch (IOException e) { 
         Log.e("IOException", e.getLocalizedMessage()); 
        } 
       } 
      }).start(); 
     } 

回答

0

畢加索將自動爲您緩存。您可以影響磁盤/內存緩存的大小。例如

long maxHeapSize = Runtime.getRuntime().maxMemory(); 

    Picasso picasso = new Picasso.Builder(context) 
      .memoryCache(new LruCache((int)maxHeapSize/3)) 
      .build(); 
+0

如果我要在離線模式下運行應用程序,它會獲取正確的圖像嗎? – Shahal