如何URLEn code proccess后加载图像 [英] How to load image after URLEncode proccess

查看:146
本文介绍了如何URLEn code proccess后加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 onPostExecute proccess,我用的是越来越图片路径从我的服务器:

 字符串图像= c.getString(Constants.TAG_Image);

在字符串图片我得到的数据是这样的: bertho joris.png 我的名字bertho.jpg

 字符串连接codedURL = URLEn coder.en code(图像,UTF-8);
字符串validImage =htt​​p://www.mydomain.com/image/\"+en$c$cdURL;

但是,当我使用 URLEn codeR 功能,我的图片没有出现在列表视图。我使用的是类 ImageLoader的

 进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.net.HttpURLConnection中;
进口的java.net.URL;
进口java.util.Collections中;
进口的java.util.Map;
进口java.util.WeakHashMap中;
进口java.util.concurrent.ExecutorService中;
进口java.util.concurrent.Executors;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.widget.ImageView;进口com.berthojoris.bacaberita.R;公共类ImageLoader的{    的MemoryCache的MemoryCache =新的MemoryCache();
    FileCache fileCache;
    私人地图< ImageView的,字符串> imageViews = Collections.synchronizedMap(新的WeakHashMap< ImageView的,字符串>());
    ExecutorService的ExecutorService的;    公共ImageLoader的(上下文的背景下){
        fileCache =新FileCache(背景);
        ExecutorService的= Executors.newFixedThreadPool(5);
    }    最终诠释stub_id = R.drawable.not_found;
    公共无效DisplayImage(字符串URL,ImageView的ImageView的)
    {
        imageViews.put(ImageView的,URL);
        位图位图= memoryCache.get(URL);
        如果(位图!= NULL)
            imageView.setImageBitmap(位图);
        其他
        {
            queuePhoto(URL,ImageView的);
            imageView.setImageResource(stub_id);
        }
    }    私人无效queuePhoto(字符串URL,ImageView的ImageView的)
    {
        PhotoToLoad P =新PhotoToLoad(URL,ImageView的);
        executorService.submit(新PhotosLoader(P));
    }    私人位图getBitmap(字符串URL)
    {
        文件F = fileCache.getFile(URL);        //从SD缓存
        位图B =去codeFILE(F);
        如果(B!= NULL)
            返回b;        //从网页
        尝试{
            位图位图= NULL;
            URL =图片网址新的URL(网址);
            HttpURLConnection的康恩=(HttpURLConnection类)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(真);
            InputStream为= conn.getInputStream();
            OutputStream的OS =新的FileOutputStream(F);
            Utils.CopyStream(是,OS);
            os.close();
            位=去codeFILE(F);
            返回位图;
        }赶上(例外前){
           ex.printStackTrace();
           返回null;
        }
    }    //德$ C $连拍影像的鳞它来减少内存消耗
    私人位图德codeFILE(文件f){
        尝试{
            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(新的FileInputStream(F),空,O);            //找到正确的比例值。它应该是2的幂。
            最终诠释REQUIRED_SIZE = 70;
            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                    打破;
                width_tmp / = 2;
                height_tmp / = 2;
                规模* = 2;
            }            //德code。与inSampleSize
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;
            返回BitmapFactory.de codeStream(新的FileInputStream(F),空,O2);
        }赶上(FileNotFoundException异常五){}
        返回null;
    }    //任务队列
    私有类PhotoToLoad
    {
        公共字符串的URL;
        公共ImageView的ImageView的;
        公共PhotoToLoad(字符串U,ImageView的我){
            URL = U;
            ImageView的= I;
        }
    }    类PhotosLoader实现Runnable {
        PhotoToLoad photoToLoad;
        PhotosLoader(PhotoToLoad photoToLoad){
            this.photoToLoad = photoToLoad;
        }        @覆盖
        公共无效的run(){
            如果(imageViewReused(photoToLoad))
                返回;
            BMP位图= getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url,BMP);
            如果(imageViewReused(photoToLoad))
                返回;
            BitmapDisplayer BD =新BitmapDisplayer(BMP,photoToLoad);
            活动一=(活动)photoToLoad.imageView.getContext();
            a.runOnUiThread(BD);
        }
    }    布尔imageViewReused(PhotoToLoad photoToLoad){
        字符串标记= imageViews.get(photoToLoad.imageView);
        如果(标记== NULL ||!tag.equals(photoToLoad.url))
            返回true;
        返回false;
    }    //用在UI线程来显示位图
    类BitmapDisplayer实现Runnable
    {
        位图位图;
        PhotoToLoad photoToLoad;
        公共BitmapDisplayer(位图B,PhotoToLoad P){位= B; photoToLoad = P;}
        公共无效的run()
        {
            如果(imageViewReused(photoToLoad))
                返回;
            如果(位图!= NULL)
                photoToLoad.imageView.setImageBitmap(位图);
            其他
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }    公共无效clearCache(){
        memoryCache.clear();
        fileCache.clear();
    }}

我的问题:


  1. 为什么我的画不出来的列表视图?

  2. 有没有少用这种方式来显示图像?

  3. 什么我需要我的所有照片都习惯于出现?


解决方案

我用这个类,它是工作。

您调用该函数( loader.DisplayImage(URL,ImageView的))的 onPostExecute 功能异步 ..类?

如果它的确定,然后检查你的图片URL(请写出图像URL日志和控制它,因为我犯了那个错误一次)。

In my onPostExecute proccess, I was getting Image Path from my server using :

String image = c.getString(Constants.TAG_Image);

In the string image I get the data like this : bertho joris.png or my name bertho.jpg

String encodedURL = URLEncoder.encode(image,"UTF-8");
String validImage = "http://www.mydomain.com/image/"+encodedURL;

But when I use the URLEncoder function, my images do not appear on the listview. I use a class ImageLoader :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.berthojoris.bacaberita.R;

public class ImageLoader {

    MemoryCache memoryCache=new MemoryCache();
    FileCache fileCache;
    private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;

    public ImageLoader(Context context){
        fileCache=new FileCache(context);
        executorService=Executors.newFixedThreadPool(5);
    }

    final int stub_id = R.drawable.not_found;
    public void DisplayImage(String url, ImageView imageView)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
        else
        {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }
    }

    private void queuePhoto(String url, ImageView imageView)
    {
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url)
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

    //Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i){
            url=u;
            imageView=i;
        }
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;
        PhotosLoader(PhotoToLoad photoToLoad){
            this.photoToLoad=photoToLoad;
        }

        @Override
        public void run() {
            if(imageViewReused(photoToLoad))
                return;
            Bitmap bmp=getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if(imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad){
        String tag=imageViews.get(photoToLoad.imageView);
        if(tag==null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;
        public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
        public void run()
        {
            if(imageViewReused(photoToLoad))
                return;
            if(bitmap!=null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

}

My question:

  1. Why is my picture did not come out on listview?
  2. Is there less to display the image in this way?
  3. What do I need to have all my pictures were accustomed to appear?

解决方案

I used this class and it is working.

Are you calling this function(loader.DisplayImage(url,imageView)) in onPostExecute function of Async.. class?

If it's ok, then check your image URL(please write image URL in log and control it, because I made that mistake one time).

这篇关于如何URLEn code proccess后加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆