我工作的这是使用JSON采取从互联网上内容的Android应用程序,我使用的服务加载JSON在本地数据库中,我有两个主要的疑虑:Android的动态图像加载
1 - 如何分辨数据库加载新数据以重新加载显示的应用程序。
2-我有存储在数据库中的图像URL,需要按需显示,以显示我已经用progressBar和Image视图扩展了默认的FrameLayout,如果图像是新的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();
}
}
部分正确appering剩下的就是显示不正确的像素车轮,被损坏的图像显示的是,当你下载等与下载站中电脑,你看到未下载的部分黑色。 – Nullity 2012-04-03 08:08:12