Android DownloadManager正在将文件保存到设备,但无法打开它
我试图打开保存到设备上的文件,但无法打开。设备指示无法打开此文档 这是我的密码。该文件为.ppt格式 显然,我在清单中有以下权限:Android DownloadManager正在将文件保存到设备,但无法打开它,android,android-external-storage,Android,Android External Storage,我试图打开保存到设备上的文件,但无法打开。设备指示无法打开此文档 这是我的密码。该文件为.ppt格式 显然,我在清单中有以下权限: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 好的,我找到了解决方案,我会把它贴在
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
好的,我找到了解决方案,我会把它贴在这里,也许它会在将来帮助其他人 基本上,在调试和大量日志的帮助下。 我最终发现DownloadManager会自动将文件保存到以下路径: 存储/模拟/0/Android/data/+包名+/files/ 在他为你创造的所有道路之后,我添加了我自己的道路。 因此,我正在查找的文件路径不正确
我所做的是将我自己的文件与下载管理器的正确路径相匹配。使用StartActivityContent.CreateChooseContent,打开文件。。。;这可能显示没有找到要打开文件的应用程序。有什么区别?action_视图已经打开了一个弹出窗口,其中包含可以打开它的程序列表,但我已经尝试了两个程序,POLARIS Office Viewer 5和金山办公软件。两人都应该能够打开该文件,但他们都编写了此文档,因此无法打开。是否可以使用SD卡浏览器手动查看该文件?否,它会给出相同的错误=\
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);
}