انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
ارور outOfMemory در کلاس ImageDownloader

ارور outOfMemory در کلاس ImageDownloader

#1
سلام دوستان
این کلاسی که برای imageDownloader توی سایت هست
کلید 295
وقتی تعداد عکسای زیادی رو بخواد دانلود کنه بعد از مدتی پیغام outOfMemory میده ... کلاسی هست که بشه تصاویر رو ریسایز کرد یا نمیدونم کاری کرد که تصاویر با حجم زیاد رو اول کوچیک کنه بعد دانلود کنه یا یه جوری توی یک بازه زمانی حافظه برگردانده بشه؟
پاسخ

ارور outOfMemory در کلاس ImageDownloader

#2
به نظرتون این کلاس نیاز رو برطرف میکنه؟؟؟
کد پی‌اچ‌پی:
import java.util.Map
import java.util.HashMap
import java.util.LinkedList
import java.util.Collections
import java.util.WeakHashMap
import java.lang.ref.SoftReference
import java.util.concurrent.Executors
import java.util.concurrent.ExecutorService
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.os.Handler;
import android.os.Message;
import java.io.InputStream;
import java.net.MalformedURLException
import java.io.IOException
import java.net.URL;
import java.net.URLConnection;

public class 
DrawableBackgroundDownloader {    

private final 
Map<StringSoftReference<Drawable>> mCache = new HashMap<StringSoftReference<Drawable>>();   
private final 
LinkedList <DrawablemChacheController = new LinkedList <Drawable> ();
private 
ExecutorService mThreadPool;  
private final 
Map<ImageViewStringmImageViews Collections.synchronizedMap(new WeakHashMap<ImageViewString>());  

public static 
int MAX_CACHE_SIZE 80
public 
int THREAD_POOL_SIZE 3;

/**
 * Constructor
 */
public DrawableBackgroundDownloader() {  
    
mThreadPool Executors.newFixedThreadPool(THREAD_POOL_SIZE);  
}  


/**
 * Clears all instance data and stops running threads
 */
public void Reset() {
    
ExecutorService oldThreadPool mThreadPool;
    
mThreadPool Executors.newFixedThreadPool(THREAD_POOL_SIZE);
    
oldThreadPool.shutdownNow();

    
mChacheController.clear();
    
mCache.clear();
    
mImageViews.clear();
}  

public 
void loadDrawable(final String url, final ImageView imageView,Drawable placeholder) {  
    
mImageViews.put(imageViewurl);  
    
Drawable drawable getDrawableFromCache(url);  

    
// check in UI thread, so no concurrency issues  
    
if (drawable != null) {  
        
//Log.d(null, "Item loaded from mCache: " + url);  
        
imageView.setImageDrawable(drawable);  
    } else {  
        
imageView.setImageDrawable(placeholder);  
        
queueJob(urlimageViewplaceholder);  
    }  



private 
Drawable getDrawableFromCache(String url) {  
    if (
mCache.containsKey(url)) {  
        return 
mCache.get(url).get();  
    }  

    return 
null;  
}

private 
synchronized void putDrawableInCache(String url,Drawable drawable) {  
    
int chacheControllerSize mChacheController.size();
    if (
chacheControllerSize MAX_CACHE_SIZE
        
mChacheController.subList(0MAX_CACHE_SIZE/2).clear();

    
mChacheController.addLast(drawable);
    
mCache.put(url, new SoftReference<Drawable>(drawable));

}  

private 
void queueJob(final String url, final ImageView imageView,final Drawable placeholder) {  
    
/* Create handler in UI thread. */  
    
final Handler handler = new Handler() {  
        @
Override  
        
public void handleMessage(Message msg) {  
            
String tag mImageViews.get(imageView);  
            if (
tag != null && tag.equals(url)) {
                if (
imageView.isShown())
                    if (
msg.obj != null) {
                        
imageView.setImageDrawable((Drawablemsg.obj);  
                    } else {  
                        
imageView.setImageDrawable(placeholder);  
                        
//Log.d(null, "fail " + url);  
                    

            }  
        }  
    };  

    
mThreadPool.submit(new Runnable() {  
        @
Override  
        
public void run() {  
            final 
Drawable bmp downloadDrawable(url);
            
// if the view is not visible anymore, the image will be ready for next time in cache
            
if (imageView.isShown())
            {
                
Message message Message.obtain();  
                
message.obj bmp;
                
//Log.d(null, "Item downloaded: " + url);  

                
handler.sendMessage(message);
            }
        }  
    });  
}  



private 
Drawable downloadDrawable(String url) {  
    try {  
        
InputStream is getInputStream(url);

        
Drawable drawable Drawable.createFromStream(isurl);
        
putDrawableInCache(url,drawable);  
        return 
drawable;  

    } catch (
MalformedURLException e) {  
        
e.printStackTrace();  
    } catch (
IOException e) {  
        
e.printStackTrace();  
    }  

    return 
null;  
}  


private 
InputStream getInputStream(String urlStringthrows MalformedURLExceptionIOException {
    
URL url = new URL(urlString);
    
URLConnection connection;
    
connection url.openConnection();
    
connection.setUseCaches(true); 
    
connection.connect();
    
InputStream response connection.getInputStream();

    return 
response;
}

 اگر بهتر هست لطفاً بگید چطور باید فراخوانی بشه و به iimageView متصل بشه؟
 
پاسخ
 سپاس شده توسط شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان