를로드 안드로이드, 나는 두 가지 의심이 DB에 새로운 데이터가로드되어 디스플레이를 다시로드하는 응용 프로그램입니다., 나는 서비스를 사용하여 로컬 데이터베이스에 JSON을 동적 이미지 로딩
2 DB에 이미지 URL이 저장되어있는 상태에서 progressBar 및 Image view로 기본 FrameLayout을 확장 한 것을 표시하기 위해 이미지가있는 경우 progressbar가 표시됩니다. 아직로드되지 않았고 이미지가로드되면 표시 될 것입니다. 또한 새로운 FreamLayout은 파일 시스템에 이미지가 있음을 확인하는 AsyncTask를 확장하는 클래스를 가지고 있습니다. 이미지가 존재하지 않으면 이미지 다운로드가 수행됩니다. 아래에 제가 한 클래스의 샘플이 있습니다. 이것이 올바른 방법인가? 그리고이 경우 다운로드하는 동안 이미지가 손상되어 어떻게 문제를 해결할 수 있습니까?
도움을 주셔서 감사합니다.
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되고 나머지는 정확하지 픽셀을 표시하고 :) 바퀴를 재발견하지 마십시오, 손상된 이미지는 다운로드 할 때처럼 표시하고 다운로드 정지에 있습니다
금 OLE이다 PC의 경우, 다운로드되지 않은 부품이 보입니다. – Nullity