[英]Android dynamic image loading
我正在开发使用JSON从互联网上获取内容的Android应用程序,我使用服务将JSON加载到本地数据库中,我主要有两个疑问:
1-如何告诉应用程序数据库已加载新数据以重新加载显示。
2-我将图像URL存储在数据库中,需要按原样显示,以显示已使用progressBar和Image视图扩展了默认的FrameLayout,如果尚未加载Image,则新的Frame Layout将显示progressBar,并且如果加载了图像,则将显示该图像,此外,新的FreamLayout具有扩展AsyncTask的类,该类接受URL,以检查图像在文件系统上是否存在,如果不存在,则进行图像下载。 以下是我完成的课程的示例。 这是正确的做法吗? 在这种情况下,有些图像在下载时会损坏,该如何解决该问题?
感谢您的协助。
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();
}
}
我在github.com(nostra13 / Android-Universal-Image-Loader(Java))上发现了这个很棒的库,它很完美,并且可以通过诸如在磁盘和内存上缓存以及兑现大小之类的选项来完成工作。
最重要的是:不要重新发明轮子:)
对于第一个问题
第二个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.