首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android动态图片加载

Android动态图片加载
EN

Stack Overflow用户
提问于 2012-04-03 15:43:31
回答 2查看 681关注 0票数 1

我在使用JSON从互联网获取内容的Android应用程序上工作,我使用一个服务将JSON加载到本地数据库中,我有两个主要的疑问:

1-如何告诉应用程序DB已加载新数据以重新加载显示。

2-我有图像网址存储在数据库中,这需要显示将,以显示,我已经扩展了默认的FrameLayout与progressBar和图像视图,新的框架布局将显示progressBar,如果图像还没有加载,如果图像加载它将被显示,而且新的FreamLayout有一个扩展AsyncTask的类,它采取网址检查图像是否存在于文件系统上,如果它不存在,图像下载发生。下面是我完成的类的示例。这是正确的做法吗?在这种情况下,我有一些图像在下载时被损坏,如何解决这个问题?

谢谢你的帮助。

代码语言:javascript
复制
    public class ImageLoader extends FrameLayout
    {
private String imageURL;
private ImageView img;
private ProgressBar pb;
private boolean isLoaded;
File rootDir = new File("/data/data/com.ait.kw.pharmacy/files");
private static final String TAG = "FrameLayoutExpander";

    //defining file name and url
    public String fileName = "";
    public String fileURL = "";

public ImageLoader(Context context , AttributeSet attr) {
    super(context,attr);
    isLoaded = false;
    img = new ImageView(context , null);
    pb = new ProgressBar(context,null , android.R.attr.progressBarStyle);
    img.setVisibility(View.INVISIBLE);
    pb.setVisibility(View.VISIBLE);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    super.addView(img,params);
    super.addView(pb,params);
    checkAndCreateDirectory("/images");

}

public ImageLoader(Context context, AttributeSet attr, int defaultStyle)
{
    super(context, attr, defaultStyle);
    isLoaded = false;
    img = new ImageView(context , null);
    pb = new ProgressBar(context,null , android.R.attr.progressBarStyle);
    img.setVisibility(View.INVISIBLE);
    pb.setVisibility(View.VISIBLE);
    super.addView(img);
    super.addView(pb);
    checkAndCreateDirectory("/images");
    isLoaded = checkImaeExists(rootDir.getAbsolutePath()+"/images/"+fileName);
  }
public void setImageResource(int resId) {
    pb.setVisibility(View.GONE);
    img.setVisibility(View.VISIBLE);
    img.setImageResource(resId);
  }

public void setImageDrawable(Drawable drawable) {
    pb.setVisibility(View.GONE);
    img.setVisibility(View.VISIBLE);
    img.setImageDrawable(drawable);
  }

public void startLoad(String url)
{
    setImageURL(url);
    loadImage();
}

public void setImageURL(String url)
{
    imageURL = url;
    fileName = imageURL.substring(imageURL.lastIndexOf("/")+1);
    isLoaded = checkImaeExists(rootDir.getAbsolutePath()+"/images/"+fileName);
}
public void loadImage() 
{
    if(! isLoaded)
    {
        DownloadFileAsync d = new DownloadFileAsync();
        d.execute(imageURL);
    }
    else
    {
        Drawable d = Drawable.createFromPath(rootDir + "/images/" + fileName);
        setImageDrawable(d);
    }
}
//this is our download file asynctask
class DownloadFileAsync extends AsyncTask<String, String, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


    @Override
    protected String doInBackground(String... aurl) {

        try {
            //connecting to url
            URL u = new URL(imageURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            //lenghtOfFile is used for calculating download progress
            int lenghtOfFile = c.getContentLength();

            //this is where the file will be seen after the download
            FileOutputStream f = new FileOutputStream(new File(rootDir + "/images/", fileName));
            //file input is from the url
            InputStream in = c.getInputStream();

            //here's the download code
            byte[] buffer = new byte[1024];
            int len1 = 0;
            long total = 0;

            while ((len1 = in.read(buffer)) > 0) {
                total += len1; //total = total + len1
                publishProgress("" + (int)((total*100)/lenghtOfFile));
                f.write(buffer, 0, len1);
            }
            f.close();

        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(String unused) 
    {
        //dismiss the dialog after the file was downloaded
        isLoaded = true;
        Drawable d = Drawable.createFromPath(rootDir + "/images/" + fileName);
        setImageDrawable(d);
    }
}

//function to verify if directory exists
public void checkAndCreateDirectory(String dirName){
    File new_dir = new File( rootDir + dirName );
    if( !new_dir.exists() ){
        new_dir.mkdirs();
    }
}

public boolean checkImaeExists(String filename)
{
    File file = new File(filename);

    return file.exists();

}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-22 13:25:43

我在github.com (nostra13 / Android-Universal-Image-Loader (Java))上发现了这个很棒的库,它非常完美,可以选择缓存磁盘、内存和缓存大小。

金色的奥利是:不要重复发明轮子:)

票数 0
EN

Stack Overflow用户

发布于 2012-04-03 15:57:31

对于第一个问题

  • 你可以从你用来加载数据库的服务中广播一个意图...现在在您的应用程序中注册接收器。在接收器的onreceive方法中,您可以将代码放到重新加载和显示中。

对于的第二个问题

  • 您正在使用的逻辑似乎是正确的..但是我不明白你所说的“一些图片在下载过程中被破坏”是什么意思。当他们处于corrupted..?

状态时会发生什么

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9989107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档