2016-12-01 54 views
-1

我正在開發一個項目,我想從服務器獲取圖像,通過點擊按鈕單擊並顯示在我的應用程序中。在此場景中與我分享代碼。請幫幫我。從服務器通過凌空抽取圖像在android中

+0

你試過了嗎?向我們展示一些*代碼*。 –

+0

請提供您的真實問題,並提供有關錯誤的代碼和說明。如果您需要示例,請使用github.com上的搜索 –

+0

http://www.truiton.com/2015/03/android-volley-imageloader-networkimageview-example/檢查它 –

回答

0

創建這樣的方式您的緩存機制:

/** 
    * Image caching used by volley image loader 
    * Created by tasneem on 23/7/15. 
    */ 

    import android.graphics.Bitmap; 
    import android.support.v4.util.LruCache; 

    import com.android.volley.toolbox.ImageLoader.ImageCache; 

    public class LruBitmapCache extends LruCache<String, Bitmap> implements 
      ImageCache { 
     public static int getDefaultLruCacheSize() { 
      final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 
      final int cacheSize = maxMemory/8; 

      return cacheSize; 
     } 

     public LruBitmapCache() { 
      this(getDefaultLruCacheSize()); 
     } 

     public LruBitmapCache(int sizeInKiloBytes) { 
      super(sizeInKiloBytes); 
     } 

     @Override 
     protected int sizeOf(String key, Bitmap value) { 
      return value.getRowBytes() * value.getHeight()/1024; 
     } 

     @Override 
     public Bitmap getBitmap(String url) { 
      return get(url); 
     } 

     @Override 
     public void putBitmap(String url, Bitmap bitmap) { 
      put(url, bitmap); 
     } 
    } 

,然後加入到這樣的方式您VolleyControler:

import android.app.Application; 
import android.text.TextUtils; 
import android.util.Log; 

import com.android.volley.DefaultRetryPolicy; 
import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.toolbox.ImageLoader; 
import com.android.volley.toolbox.Volley; 
import com.zingoy.app.ZingoyApplication; 

    /** 
    * Class for handling all network requests 
    * @author tasneem 
    */ 
    public class VolleyController extends Application { 

    public static final String TAG = VolleyController.class.getSimpleName(); 

    private static VolleyController mInstance; 
    private RequestQueue mRequestQueue; 
    private ImageLoader mImageLoader; 

    private VolleyController() { 
     mRequestQueue = mRequestQueue != null ? mRequestQueue : Volley.newRequestQueue(ZingoyApplication.getAppContext()); 
     mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache()); 
    } 

    public static synchronized VolleyController getInstance() { 
     mInstance = mInstance != null ? mInstance : new VolleyController(); 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     Log.d(TAG, "Cancelling request"); 
     return mRequestQueue != null ? mRequestQueue : Volley.newRequestQueue(getApplicationContext()); 
    } 

    public ImageLoader getImageLoader() { 
     return mImageLoader != null ? mImageLoader : new ImageLoader(mRequestQueue, new LruBitmapCache()); 
    } 
} 

,只要你想渲染你的形象,你可以使用:

imageLayout.setImageUrl(url, VolleyController.getInstance().getImageLoader()); 
+0

私人VolleyController() { mRequestQueue = mRequestQueue! = null? mRequestQueue:Volley.newRequestQueue(ZingoyApplication.getAppContext()); mImageLoader = new ImageLoader(mRequestQueue,new LruBitmapCache()); } 我在Volley.mRequestQueue(???)寫的....請幫助..因爲m新到android –

+0

@AliMehdi,抱歉無法回覆。你還需要幫助嗎? – Tasneem