Android SDK中 - 文件的下载速度很慢 [英] Android SDK - Downloading of files very slow

查看:261
本文介绍了Android SDK中 - 文件的下载速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建/复制下载功能,获得通过一个URL的图像和视频并下载到Android设备上。

在下载的小图片这是没有问题的。但是,当试图获取文件比 2MB 以上(通过WLAN!)它需要从字面上的年龄!约5分钟的视频25MB等。

任何想法什么可能出错?

下面是我的code:

  / *打开到URL的连接。 * /
        URLConnection的UCON = url.openConnection();        / *
         *定义InputStreams从URLConnection的阅读。
         * /
        InputStream为= ucon.getInputStream();
        二BufferedInputStream为=新的BufferedInputStream(是);        / *
         *读取字节到缓冲区,直到有没有更多阅读(-1)。
         * /
        ByteArrayBuffer BAF =新ByteArrayBuffer(50);
        INT电流= 0;
        而((电流= bis.read())!= - 1){
                baf.append((字节)电流);
        }        / *读取转换为字符串的字节数。 * /
        FOS的FileOutputStream =新的FileOutputStream(文件);
        fos.write(baf.toByteArray());
        fos.close();


解决方案

使用下载管理器下载文件

 私人长期排队​​;
私人下载管理器DM; 广播接收器接收器=新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            字符串行动= intent.getAction();
            如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
                长downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID,0);
                查询查询=新的Query();
                query.setFilterById(排队);
                光标C = dm.query(查询);
                如果(c.moveToFirst()){
                    INT参数:columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    如果(DownloadManager.STATUS_SUCCESSFUL == c.getInt(参数:columnIndex)){                        Log.e(下载完成,在这里下载完成);
                        尝试
                        {
                                                        }
                        赶上(例外五){
                            // TODO:处理异常
                        }                    }
}
            }
}};

要开始下载使用:

  DM =(下载管理器)activityContext.getSystemService(activityContext.DOWNLOAD_SERVICE);
    请求请求=新的请求(Uri.parse(URL))setDestinationInExternalPublicDir(目录,文件名);
    排队= dm.enqueue(请求);

I built/copied a download function which gets images and videos via an URL and downloads them to an Android device.

When downloading small images it's no problem. But when trying to get files with more than 2MB (via WLAN!) it takes literally ages! About 5 minutes for an 25MB video and so on.

Any ideas what might go wrong?

Here's my code:

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
                baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();

解决方案

Use DownloadManager to download files

        private long enqueue;
private DownloadManager dm;

 BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();


            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                Query query = new Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                        Log.e("Download completed","HERE DOWNLOAD COMPLETED");
                        try
                        {
                                                        }
                        catch (Exception e) {
                            // TODO: handle exception
                        }

                    }
}
            }
}};

To start this download use:

         dm = (DownloadManager)activityContext.getSystemService(activityContext.DOWNLOAD_SERVICE);
    Request request = new Request(Uri.parse("URL")).setDestinationInExternalPublicDir("DIRECTORY","FILENAME");
    enqueue = dm.enqueue(request);

这篇关于Android SDK中 - 文件的下载速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆