2014-02-18 5 views
0

10 초 비디오를 생성하고 아마존 서버에 업로드 한 후 이미지를 lazyloading과 같이 listview 내에 으로 다운로드했습니다. 처음 다운로드하는 데 6-8 분 이상 걸립니다. 비디오 파일 크기 다운로드는 에서 30 초가 소요되는 ios에서 동일한 비디오가 6.89-7 MB입니다. 내 요구 사항은 안드로이드의 포도 나무 응용 프로그램과 같습니다. 도움을 청하십시오. 미리 감사드립니다.android에서 비디오 다운로드 문제

public class VideoLoader { 

    VideoMemoryCache memoryCache = new VideoMemoryCache(); 
    VideoFileCache fileCache; 
    private Context context; 
    private Bitmap mLoadingbmp; 
    private Map<VideoView, String> imageViews = Collections 
      .synchronizedMap(new WeakHashMap<VideoView, String>()); 
    ExecutorService executorService; 
    Handler handler = new Handler(); 

    public VideoLoader(Context context) { 

     fileCache = new VideoFileCache(context); 
     this.context = context; 

     executorService = Executors.newFixedThreadPool(5); 

     mLoadingbmp = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.loading); 

    } 
    public void DisplayImage(String url, VideoView videoView, 
      Button defaultIMage,ProgressBar progressbar) { 

     imageViews.put(videoView, url); 
     File bitmap = memoryCache.get(url); 

     if (bitmap != null && bitmap.length()>0) { 

      videoView.setVideoPath(bitmap.getAbsolutePath()); 
      defaultIMage.setVisibility(View.VISIBLE); 
      progressbar.setVisibility(View.GONE); 
     } else { 
      queuePhoto(url, videoView,defaultIMage,progressbar); 

      defaultIMage.setVisibility(View.GONE); 
      progressbar.setVisibility(View.VISIBLE); 
     } 

    } 

    private void queuePhoto(String url, VideoView imageView,Button btn,ProgressBar pb) { 
     PhotoToLoad p = new PhotoToLoad(url, imageView,btn,pb); 
     executorService.submit(new PhotosLoader(p)); 
    } 

    private class PhotoToLoad { 
     public String url; 
     public VideoView imageView; 
     public Button defaultImage; 
     ProgressBar progressbar; 
     public PhotoToLoad(String u, VideoView i,Button btn,ProgressBar pb) { 
      url = u; 
      imageView = i; 
      defaultImage=btn; 
      progressbar=pb; 
     } 
    } 

    class PhotosLoader implements Runnable { 
     PhotoToLoad photoToLoad; 

     PhotosLoader(PhotoToLoad photoToLoad) { 
      this.photoToLoad = photoToLoad; 
     } 

     @Override 
     public void run() { 
      try { 
       if (imageViewReused(photoToLoad)) 
        return; 
       File bmp = getFile(photoToLoad.url); 

       memoryCache.put(photoToLoad.url, bmp); 

       if (imageViewReused(photoToLoad)) 
        return; 
     //  BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); 
     //  handler.post(bd); 

      } catch (Throwable th) { 
       th.printStackTrace(); 
      } 
     } 
    } 

    private File getFile(String path) { 
     File f = fileCache.getFile(path); 

     if (f.exists()) 
      return f; 

     try { 
      if (!URLUtil.isNetworkUrl(path)) { 
      } else { 
       URL url = new URL(path); 
       HttpURLConnection cn = (HttpURLConnection) url.openConnection(); 
       cn.setReadTimeout(5000); 
       cn.setConnectTimeout(30000); 
       cn.setInstanceFollowRedirects(true); 
       cn.connect(); 
       InputStream stream = cn.getInputStream(); 
       if (stream == null) 
        throw new RuntimeException("stream is null"); 
       f.deleteOnExit(); 
      // String tempPath = f.getAbsolutePath(); 
       FileOutputStream out = new FileOutputStream(f); 
       byte buf[] = new byte[1024]; 
       do { 
        int numread = stream.read(buf); 
        if (numread <= 0) 
         break; 
        out.write(buf, 0, numread); 
       } while (true); 
       try { 
        out.flush(); 
        out.close(); 
        stream.close(); 
        cn.disconnect(); 

       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
       } 

      return f; 

     } catch (Throwable ex) { 
      ex.printStackTrace(); 

      return null; 
     } 
    } 

    boolean imageViewReused(PhotoToLoad photoToLoad) { 

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

    class BitmapDisplayer implements Runnable { 
     File bitmap; 
     PhotoToLoad photoToLoad; 

     public BitmapDisplayer(File b, PhotoToLoad p) { 
      bitmap = b; 
      photoToLoad = p; 
     } 

     public void run() { 
      if (imageViewReused(photoToLoad)) 
       return; 

      try { 
       if (bitmap!= null && bitmap.length()>0) { 
        photoToLoad.imageView.setVideoPath(bitmap.getAbsolutePath()); 
        photoToLoad.defaultImage.setVisibility(View.VISIBLE); 
        photoToLoad.progressbar.setVisibility(View.GONE); 

       } else { 

        photoToLoad.defaultImage.setVisibility(View.GONE); 
        photoToLoad.progressbar.setVisibility(View.VISIBLE); 
       } 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

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

이 질문은 [CodeReview] (http://codereview.stackexchange.com/)에 더 적합하다고 생각합니다. –

답변

0

다운로드 속도 등 당신은 다운로드 성능에 영향을 미치는 작은 데이터 버퍼를 단일 스레드 다운로드를 사용하는 네트워크 대역폭, 장치/SD 카드의 쓰기 속도, 등 많은 요인에 따라 달라집니다 -이 : 여기 내 코드입니다 . 데이터 버퍼 크기를 1024에서 8192 이상으로 늘리고 다중 스레드 다운로드를 구현하는 것이 좋습니다 (서버에서 지원하는 경우). 자세한 내용은 HTTP "Range"헤더를 참조하십시오.