我试图打开保存到设备的文件,但我无法打开它。设备显示,"此文档无法打开"。
这是我的代码。该文件是.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);
}
答案 0 :(得分:0)
好的,所以我找到了解决方案,我会在这里发布,所以也许它将来会帮助其他人。
基本上,在调试和大量日志的帮助下。 我最终发现DownloadManager会自动将文件保存到此路径:
“storage / emulated / 0 / Android / data /”+包名+“/ files /".
在他为你创造的所有道路之后,我加入了自己的道路。 所以我正在寻找文件的路径不正确。
我所做的是将我自己的文件与下载管理器的正确路径匹配。