我创建了10秒的视频并上传到亚马逊服务器上,然后我下载了它 在列表视图中,如图像延迟加载。第一次下载超过6-8分钟。视频文件大小为6.89-7 MB。但相同的视频在ios中需要30秒 下载。我的要求与android中的vine应用程序相同。请帮忙。 提前致谢 。 这是我的代码: -
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 :(得分:0)
下载速度取决于许多因素,包括您的网络带宽,设备/ SD卡写入速度等。您正在使用单线程下载和小数据缓冲区,这会影响下载性能。我建议将数据缓冲区大小从1024增加到8192或更高,并实现多线程下载(如果您的服务器支持它)。请参阅HTTP&#34;范围&#34;标题详情。