2012-08-28 1 views
4

누군가 내 코드를 확인하십시오. 내 진행 대화 상자가 나타나기도 전에 너무 빨리 나타납니다. 다운로드 과정에서 진행 상황을 보여주는 것으로 가정합니다. 그러나 내 이미지가로드되기 전에 진행 대화 상자가 닫힙니다. 때문에 이미지 로더 클래스의AsyncTask에 Android 진행률 대화 상자가 표시되지 않습니다.

public class SingleImageViewActivity extends SherlockActivity { 

     // XML node keys 
    static final String KEY_TITLE = "title"; 
    static final String KEY_ARTIST = "artist"; 
    static final String KEY_THUMB_URL = "thumb_url"; 
    static final String KEY_BIG_URL = "big_url"; 
    private ProgressDialog pDialog; 
    String title; 
    String artist; 
    //String image_url; 
    String big_image_url; 
    ImageView view; 
    Intent intent; 
    Context context; 
    ShareActionProvider mShareActionProvider; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.single_view_item); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      //checkInternetConnection(); 

      new loadSingleView().execute(); 
      view = (ImageView) findViewById(R.id.single_image);  

     } 

    public class loadSingleView extends AsyncTask<String, String, String> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(SingleImageViewActivity.this); 
       pDialog.setTitle("Connect to Server"); 
       pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 
      @Override 
      protected String doInBackground(String... args) { 
       // updating UI from Background Thread 


       Intent in = getIntent(); 

       //image_url = in.getStringExtra(KEY_THUMB_URL); 
       big_image_url = in.getStringExtra(KEY_BIG_URL); 
       title = in.getStringExtra(KEY_TITLE); 
       artist = in.getStringExtra(KEY_ARTIST); 

       SingleViewImageLoader imgLoader = new SingleViewImageLoader(getApplicationContext()); 

       imgLoader.DisplayImage(big_image_url, view); 
       return null; 

         } 
      @Override  
      protected void onPostExecute(String args) { 
       // dismiss the dialog after getting all products 
       TextView lblName = (TextView) findViewById(R.id.name_title); 
        TextView lblCost = (TextView) findViewById(R.id.name_artist); 



        lblName.setText(title); 
        lblCost.setText(artist); 

        ActionBar ab = getSupportActionBar(); 
        ab.setTitle(title); 
        ab.setSubtitle(artist); 




       pDialog.dismiss(); 


      } 


    } 

SingleViewImageLoader.class

public class SingleViewImageLoader { 

    MemoryCache memoryCache=new MemoryCache(); 
    FileCache fileCache; 
    private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>()); 
    ExecutorService executorService; 

    public SingleViewImageLoader(Context context){ 
     fileCache=new FileCache(context); 
     executorService=Executors.newFixedThreadPool(5); 
    } 

    final int stub_id = R.drawable.no_image; 
    public void DisplayImage(String url, ImageView imageView) 
    { 
     imageViews.put(imageView, url); 
     Bitmap bitmap=memoryCache.get(url); 
     if(bitmap!=null) 
      imageView.setImageBitmap(bitmap); 
     else 
     { 
      queuePhoto(url, imageView); 
      imageView.setImageResource(stub_id); 

     } 
    } 

    private void queuePhoto(String url, ImageView imageView) 
    { 
     PhotoToLoad p=new PhotoToLoad(url, imageView); 
     executorService.submit(new PhotosLoader(p)); 
    } 

    private Bitmap getBitmap(String url) 
    { 
     File f=fileCache.getFile(url); 

     //from SD cache 
     Bitmap b = decodeFile(f); 
     if(b!=null) 
      return b; 

     //from web 
     try { 
      Bitmap bitmap=null; 
      URL imageUrl = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
      conn.setConnectTimeout(30000); 
      conn.setReadTimeout(30000); 
      conn.setInstanceFollowRedirects(true); 
      InputStream is=conn.getInputStream(); 
      OutputStream os = new FileOutputStream(f); 
      Utils.CopyStream(is, os); 
      os.close(); 
      bitmap = decodeFile(f); 
      return bitmap; 
     } catch (Exception ex){ 
      ex.printStackTrace(); 
      return null; 
     } 
    } 

    //decodes image and scales it to reduce memory consumption 
    private Bitmap decodeFile(File f){ 
     try { 
      //decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

      //Find the correct scale value. It should be the power of 2. 
      final int REQUIRED_SIZE=500; 
      int width_tmp=o.outWidth, height_tmp=o.outHeight; 
      int scale=1; 
      while(true){ 
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale*=2; 
      } 

      //decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize=scale; 
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     } catch (FileNotFoundException e) {} 
     return null; 
    } 

    //Task for the queue 
    private class PhotoToLoad 
    { 
     public String url; 
     public ImageView imageView; 
     public PhotoToLoad(String u, ImageView i){ 
      url=u; 
      imageView=i; 
     } 
    } 

    class PhotosLoader implements Runnable { 
     PhotoToLoad photoToLoad; 
     PhotosLoader(PhotoToLoad photoToLoad){ 
      this.photoToLoad=photoToLoad; 
     } 

     @Override 
     public void run() { 
      if(imageViewReused(photoToLoad)) 
       return; 
      Bitmap bmp=getBitmap(photoToLoad.url); 
      memoryCache.put(photoToLoad.url, bmp); 
      if(imageViewReused(photoToLoad)) 
       return; 
      BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad); 
      Activity a=(Activity)photoToLoad.imageView.getContext(); 
      a.runOnUiThread(bd); 
     } 
    } 

    boolean imageViewReused(PhotoToLoad photoToLoad){ 
     String tag=imageViews.get(photoToLoad.imageView); 
     if(tag==null || !tag.equals(photoToLoad.url)) 
      return true; 
     return false; 
    } 

    //Used to display bitmap in the UI thread 
    class BitmapDisplayer implements Runnable 
    { 
     Bitmap bitmap; 
     PhotoToLoad photoToLoad; 
     public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;} 
     public void run() 
     { 
      if(imageViewReused(photoToLoad)) 
       return; 

      if(bitmap!=null) 
       photoToLoad.imageView.setImageBitmap(bitmap); 
      else 
       photoToLoad.imageView.setImageResource(stub_id); 

     } 
    } 

    public void clearCache() { 
     memoryCache.clear(); 
     fileCache.clear(); 
    } 



} 
+0

무엇이 SingleViewImageLoader입니까? 여기 – njzk2

+0

있습니다. 나는 내 코드를 편집했습니다 –

답변

3

그것은 또한 백그라운드에서 실행하고 onPostExecute() 메소드가 호출 될 수 있도록, 이미지를 표시하기 위해 스레드를 사용하여 진행 대화 상자를 닫습니다.

+0

당신은 내가 할 수있는 것이 아무것도 없다는 것을 의미합니까? –

+0

예, 이미지를 asynctask없이 설정하십시오. 이미지 로더 클래스도 이미지를 표시하기 위해 스레드를 사용하기 때문입니다. 그래서 AsyncTask 클래스가 필요합니다. –

+0

하지만 asynctask없이 진행 대화 상자를 표시 할 수 없습니다. –