使用downloadManager下载到外部存储器在Galaxy S7上无法正常工作

时间:2016-04-13 21:52:16

标签: android android-download-manager android-external-storage

下载到内部存储工作。

然而,当我尝试下载到外部SD卡时,状态直到2~3分钟后才会更新。 (意思是我从cursor.getLong(游标)下载了0个字节                                     .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));)。 最终在2~3分钟后下载。

通知状态也表示0%

enter image description here

private void addToDownloadManager(String sourcePath, String destFolder, String deskFileName, DownloadManager downloadManager) {
    try {
        if(sourcePath == null || sourcePath.equals(""))
            return;
        try {
            File folder = new File(destFolder);
            if (!folder.exists()) {
                folder.mkdirs();
            }
        }catch (Exception e) {
        }

        Uri Download_Uri = Uri.parse(sourcePath);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

        request.setAllowedOverRoaming(false);
        request.setTitle("title");
        request.setDescription("description");
        File destination = new File(destFolder, deskFileName);
        request.setDestinationUri(Uri.fromFile(destination));
        downloadReference = downloadManager.enqueue(request);
    } catch (Exception e) {
    }
}

private Timer mDownloadStatusTimer;

public void downloadStatusTimerSchedule() {
    if (mDownloadStatusTimer != null)
        downloadStatusTimerCancel();
    try {
        mDownloadStatusTimer = new Timer("DownloadStatusTimer");
        DownloadStatusTimer timer = new DownloadStatusTimer();
        mDownloadStatusTimer.schedule(timer, 500, 500); // 0.5 second
    } catch (Exception e) {
    }
}

public void downloadStatusTimerCancel() {
    if (mDownloadStatusTimer != null) {
        mDownloadStatusTimer.cancel();
        mDownloadStatusTimer.purge();
        mDownloadStatusTimer = null;
    }
}

private long bytes_downloaded = 0;
private long bytes_total = 0;

public class DownloadStatusTimer extends TimerTask {

    @Override
    public void run() {
        if (mDownloadManager != null) {
            DownloadManager.Query myDownloadQuery = new DownloadManager.Query();

            Cursor cursor = mDownloadManager.query(myDownloadQuery);

            bytes_downloaded = 0;
            bytes_total = 0;

            try {
                if (cursor != null && cursor.moveToFirst()) {
                    try {
                        // Get downloaded size/total size
                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
                                cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
                            // do nothing
                        } else {
                            bytes_downloaded += cursor.getLong(cursor
                                    .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                            bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        }
                    } catch (Exception e) {
                    }

                    while (cursor != null && cursor.moveToNext()) {
                        try {
                            // Get downloaded size/total size
                            if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL ||
                                    cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) {
                                // do nothing
                            } else {
                                bytes_downloaded += cursor.getLong(cursor
                                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                                bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                            }
                        } catch (Exception e) {
                        }
                    }
                } else {
                }
            } catch (Exception e) {
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

            Log.e("test", "Download size: " + bytes_downloaded + " / " + bytes_total);
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        tv_download_status.setText("Download size: " + bytes_downloaded + " / " + bytes_total);
                    } catch (Exception e) {
                    }
                }
            });
        }
    }
}

我测试了Note 4和Galaxy S5,它们看起来很好。

可能是Android 6.0的东西吗?还是S7? 这是一个错误吗?或者我在这里做错了什么?

如果你想自己测试一下,这是带有源代码的项目: https://drive.google.com/open?id=0BygTefPD845LTkp5QU1mOHRkMDQ

这是APK测试: https://drive.google.com/open?id=0By...Ec1ZHk5ZWRkWWc

enter image description here

[编辑]

我的目的地是:/ storage / 806E-1A11 / Android / data / com.joshua.externalsddownloadtest / files / download / video.mp4

来源是:http://downloads.4ksamples.com/downloads/[2160p]%204K-HD.Club-2013-Taipei%20101%20Fireworks%20Trailer%20(4ksamples.com).mp4

(我从Mp4示例网站获得了URL)

        File[] files = ContextCompat.getExternalFilesDirs(MainActivity.this, null);
    destFolder = null;
    for(int i=0; i< files.length; i++) {
        if (!files[i].getAbsolutePath().equals(MainActivity.this.getExternalFilesDir(null).getPath())) {
            destFolder = files[i].getAbsolutePath();
            break;
        }
    }
    boolean bDownloadToExternal = false;
    if(destFolder == null) {
        tv_download_destination.setText("No external storage found");
    } else {
        destFolder += "/download/";
        tv_download_destination.setText("Destination location: " + destFolder + fileName);
        bDownloadToExternal = true;
    }

1 个答案:

答案 0 :(得分:1)

我认为它关于android 6的新权限系统。 您应该在运行时询问权限,如:

if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != getPackageManager().PERMISSION_GRANTED){
               ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, YOUR_CONST_REQUEST_CODE);
           }
  }

实现监听器

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case YOUR_CONST_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// invoke your download method

                    } else {
// if permissions was not granted
                    }

                } 
            }
    }

其他方法可以在gradle文件中将targetSdkVersion设置为小于23。

有关您可以在此处阅读的新权限系统的更多信息Android.com