异步加载图片

1第一步在getview中:

String videoUrl =ltVideos.get(position).getPath();

        

Bitmap cachedImage = asyncIThumbnailLoader.loadCacheThumbnail(videoUrl);

                                                    

if(cachedImage!=null)

{                        

         img.setBackgroundDrawable(null);

         img.setImageBitmap(cachedImage);                                      

}

else //没找到缓存设置默认图片

{

         //Log.d("sky","cachedImagefalse");

         img.setBackgroundDrawable(null);

         img.setImageResource(R.drawable.default_poster);

}

asyncIThumbnailLoader

 /**

    * 检查缓存中的图片

    * containsKey() 方法来检测是否数据是否存在,在同步情况下,可以大大降低消耗.

    */  

    public synchronized Bitmap loadCacheThumbnail(finalString imageUrl)

    {

             if(imageCache.containsKey(imageUrl))

             { 

            Bitmap bitmap = imageCache.get(imageUrl);  //缓存中有的话,直接去取

              return  bitmap;

        } 

   return null;  

    }      

2//onCreate 建立U盘插入广播,在onReceive中,建立线程

private void setSDCardReceiver(){                 

                          broadcastRec= new BroadcastReceiver() { 

public void onReceive(Context context,Intent intent) {     

  if(Intent.ACTION_MEDIA_REMOVED.equals(intent.getAction())

         ||Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())

       || Intent.ACTION_MEDIA_BAD_REMOVAL.equals(intent.getAction())

         ||Constants.ACTION_NEWPVR_TSFILE.equals(intent.getAction()))

  {  

private void localKillGetThumbnailThread()

                  {

                           if(localGetThumbnailThreadRunable!=null)

                          {

                                   localGetThumbnailThreadRunable.stop();

                          }      

                  }

  private void localCreateGetThumbnailThread()

                  {               

                          localKillGetThumbnailThread();

                          localGetThumbnailThreadRunable = newLocalGetThumbnailThreadRunable();

                          newThread(localGetThumbnailThreadRunable).start();                      

                  }

}

3,线程的run方法中

private class LocalGetThumbnailThreadRunableimplements Runnable{           

                          privateboolean stopFlag = false;

                          publicvoid stop()

                          {

                                   stopFlag= true;

                          }

                          publicvoid run()

                          {

                                   booleannRet;

                                   if(visualVideoCount == 0)

                                   {

                                            visualVideoCount= 18;

                                   }

                                   endVideoPos= startVideoPos + visualVideoCount;

                                   if(endVideoPos > gvVideo.getCount())

                                   {

                                            endVideoPos= gvVideo.getCount();

                                   }                        

                                   if(ltVideos == null||ltVideos.size()==0)

                                   {

                                            return;

                                   }

                                   //从一个位置处开始加载。。。

                                   for(inti= startVideoPos;i<endVideoPos;i++)

                                   {

                                            if(stopFlag)

                                            {

                                                     break;

                                            }

                                            try

                                            {

                                                     StringvideoUrl = ltVideos.get(i).getPath();

                                                     BitmapcachedImage = asyncIThumbnailLoader.loadCacheThumbnail(videoUrl);

 

                                                         if(cachedImage==null)//缓存中没有的话

                                                     {

                                                             StringvideoId=""+ltVideos.get(i).getId();

                                                             cachedImage= adGvVideo.loadNoCacheThumbnailForAdapter(MainVideoPage.this,videoUrl,videoId);

                                                             if(cachedImage == null)

                                                             {                                                  

                                                                      nRet= adGvVideo.loadNoCacheThumbnailBlockingForAdapter(MainVideoPage.this,videoUrl,videoId);

                                                             }

                                                             else

                                                             {

                                                             }                                                                            

                                                     }

                                                        if(cachedImage==null)

                                                        {

                                                                 Log.d("sky","cachedImageis null");

                                                        }

                                                     myHandler.post(newRunnable() {

                                                             publicvoid run() {           

                                                                      try

                                                                      {

                                                                               adGvVideo.notifyDataSetChanged();   

                                                                      }

                                                                      catch(Exceptione)

                                                                      {

                                                                               e.printStackTrace();

                                                                      }

                                                             }

                                                     });

                                            }

                                            catch(Exception e)

                                            {

                                                     e.printStackTrace();

                                            }

                                   }       // end for                                           

                          }

                  }

loadNoCacheThumbnailForAdapter函数和loadNoCacheThumbnailBlockingForAdapter

 

 

public synchronized boolean loadNoCacheThumbnailBlockingForAdapter(finalActivity context,final String Url, final String videos_id)

                          {

                                   returnasyncIThumbnailLoader.loadNoCacheThumbnailBlocking(context,Url,videos_id);

                          }      

                         

                          publicsynchronized Bitmap loadNoCacheThumbnailForAdapter(finalActivity context,final String Url, final String videos_id)

                          {

                                   returnasyncIThumbnailLoader.loadNoCacheThumbnail(context,Url,videos_id);

                          }

 

   public  Bitmap loadNoCacheThumbnail(final Activity context,finalString Url, final String videos_id)

   { 

             //msgObj obj = new msgObj();

             //obj.imageUrl = Url;

             //obj.imageBmp =loadThumbnailFromDCIM(context,videos_id);                               

         Bitmap  imageBmp = loadThumbnailFromDCIM(context,videos_id);               

       if(imageBmp!=null)

       {

               //从消息池里返回一个新的Message,比new一个新的消息更高效

               //Message message =imageLoadhandler.obtainMessage(0, obj); 

               //imageLoadhandler.sendMessage(message);

               imageCache.put(Url,imageBmp);

       }   

       return imageBmp;

 

  public static Bitmap loadThumbnailFromDCIM(Activitycontext,String videos_id) { 

          Bitmap bitmap = null;

                 ContentResolverresolver=context.getContentResolver();

                 CursorthumbCursor=resolver.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,

                                  newString[]{"video_id","_data"}, null, null, null);

               ////thumbCursor.getString(1)*/ 

         if(thumbCursor != null)

         {

                while(thumbCursor.moveToNext())

                {

                         inttempid = thumbCursor.getInt(0);

                         Stringsid = ""+tempid;

                         if(sid.equals(videos_id))

                         {                              

                                           bitmap= BitmapFactory.decodeFile(thumbCursor.getString(1));

                                 break;

                         }

                }

         }

        

         if(thumbCursor != null)

         {

                  thumbCursor.close();

         }

              

         return bitmap;   } 

 

public staticBitmap loadThumbnailFromMediastoreBlocking(Activitycontext,String videos_id) { 

           Bitmap bitmap = null;

           Uri baseUri =MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI;

           ContentResolvercr=context.getContentResolver();

           String[] PROJECTION = new String[]{"video_id","_data"};

           //阻塞方式获取thumbnail   blocking

           Uri blockingUri =baseUri.buildUpon().appendQueryParameter("blocking", "1")

                         .appendQueryParameter("orig_id",String.valueOf(videos_id))

                         .appendQueryParameter("group_id",String.valueOf(MediaStore.Video.Thumbnails.MINI_KIND)).build();

                 CursorthumbCursor=cr.query(blockingUri, PROJECTION, null, null, null);

                 ////thumbCursor.getString(1)*/ 

        if(thumbCursor != null)

        {

                 while(thumbCursor.moveToNext())

                 {

                          inttempid = thumbCursor.getInt(0);

                          Stringsid = ""+tempid;

                          if(sid.equals(videos_id))

                          {

                                  //vm.setThumbnailPath(thumbCursor.getString(1));

         bitmap=MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),tempid,

                               MediaStore.Video.Thumbnails.MINI_KIND,null);          //Log.i("loadThumbnailFromMediastore","******thumbnailPath3333333333333="+thumbCursor.getString(1));

                                  break;

                          }

                 }

        }

       

                 if(thumbCursor!= null)

                 {

                         thumbCursor.close();

                 }

                

        return bitmap; 

    }   

你可能感兴趣的:(异步加载图片)