我工作的這是使用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