Android DownloadManager 正在将文件保存到设备但无法打开它

Android DownloadManager saving file to device and can't open it

我正在尝试打开保存到设备中的文件,但无法打开。设备指示,"This document cannot be opened".

这是我的代码。文件为.ppt格式。

显然我在清单中有这些权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

代码如下:

String fileName = doc.getName();

String packageName = fragment.getActivity().getPackageName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";

File saveFile = new File(path + fileName);

File parentDest = saveFile.getParentFile();
if (!parentDest.exists()) {
    parentDest.mkdirs(); //make all the directory structures needed
}
if (!saveFile.exists()) {

    try {
        saveFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Toast.makeText(getContext(), "Downloading...", Toast.LENGTH_SHORT).show();

    downloadingDocs.add(doc);

    final DownloadManager downloadManager;

    final long myDownloadReference;

    downloadManager = (DownloadManager) fragment.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

    Uri uri = Uri.parse(url);

    DownloadManager.Request request = new DownloadManager.Request(uri);

    request.addRequestHeader("Authorization", "Basic " + SharedPref.getAuthPrefValue());
    request.addRequestHeader("Device", BaseApplication.getCurrentDevice().getDevice().toString());
    request.addRequestHeader("DeviceId", BaseApplication.getCurrentDevice().getDeviceId());
    request.setDescription(fileName).setTitle("Downloading Document");
    request.setDestinationInExternalFilesDir(getContext(), path, fileName);
    request.setVisibleInDownloadsUi(true);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);

    myDownloadReference = downloadManager.enqueue(request);

    finalHolder1.downloadProgress.setVisibility(View.VISIBLE);

    new Thread(new Runnable() {
        @Override
        public void run() {
            downloading = true;

            while (downloading) {
                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(myDownloadReference);
                Cursor cursor = downloadManager.query(q);
                cursor.moveToFirst();
                int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                    downloadingDocs.remove(doc);
                }
                //final double dl_progress = (bytes_downloaded / bytes_total) * 100;
                final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
                fragment.getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    finalHolder2.downloadProgress.setProgress((int) dl_progress);
                }
            });
            cursor.close();
        }
    }}).start();
} else {
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(saveFile), "application/vnd.ms-powerpoint");

    fragment.getActivity().startActivity(intent);
}

好的,所以我找到了解决方案,我会 post 把它放在这里,也许将来它会对其他人有所帮助。

基本上,在调试和大量日志的帮助下。 我最终发现 DownloadManager 会自动将文件保存到这个路径:

"storage/emulated/0/Android/data/" + 包名 + "/files/".

在他为你创造的所有道路之后,我添加了我自己的道路。 所以我查找文件的路径不正确。

我所做的是将我自己的文件与下载管理器的正确路径匹配。