实际上,我需要从我的应用程序中打开默认下载文件夹。是否可能?如果是,请提供一些参考资料。
我可以通过以下方式获取下载文件夹的路径:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
非常感谢任何帮助。
实际上,我需要从我的应用程序中打开默认下载文件夹。是否可能?如果是,请提供一些参考资料。
我可以通过以下方式获取下载文件夹的路径:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
非常感谢任何帮助。
Intent
来显示最近的下载活动。startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
自API 9起可用
三星解决方案:
public static void openDownloads(@NonNull Activity activity) {
if (isSamsung()) {
Intent intent = activity.getPackageManager()
.getLaunchIntentForPackage("com.sec.android.app.myfiles");
intent.setAction("samsung.myfiles.intent.action.LAUNCH_MY_FILES");
intent.putExtra("samsung.myfiles.intent.extra.START_PATH",
getDownloadsFile().getPath());
activity.startActivity(intent);
}
else activity.startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
public static boolean isSamsung() {
String manufacturer = Build.MANUFACTURER;
if (manufacturer != null) return manufacturer.toLowerCase().equals("samsung");
return false;
}
public static File getDownloadsFile() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
通过反编译三星我的文件应用程序的dex文件,观察MainActivity如何处理意图而发现。
public static final String MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME = "samsung.myfiles.intent.action.LAUNCH_MY_FILES";
public static final String NOTIFICATION_START_PATH = "samsung.myfiles.intent.extra.START_PATH";
public static final String START_PATH = "FOLDERPATH";
if (Constant.MYFILES_NOTIFICATION_LAUNCH_INTENT_NAME.equals(intent.getAction())) {
String startNotificationPath = intent.getStringExtra(Constant.NOTIFICATION_START_PATH);
if (startNotificationPath != null) {
Bundle newArgument = new Bundle();
newArgument.putString(Constant.START_PATH, startNotificationPath);
if (new File(startNotificationPath).exists()) {
startBrowser(513, newArgument);
}
}
intent.removeExtra(Constant.NOTIFICATION_START_PATH);
}
我曾经在三星和LG手机上遇到过类似的问题,无法在下载文件夹中显示文件。后来我只需为DownloadManager添加一个文件参数“downloaded”,就可以在下载文件夹中看到文件了。
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true,
"application", file.getPath(), file.length(), true);