如何从 SharedPreferences 存储位置设置下载管理器下载位置

时间:2020-12-30 19:31:19

标签: java android

我使用 ChooserDialog 选择下载路径

就是这样

final String defaultDir = Environment.getExternalStorageState();

Button BtnSetPath = (Button)SettingsFragView.findViewById(R.id.btnSetPath);
        BtnSetPath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ChooserDialog(getContext())
                        .withFilter(true, false)
                        .withStartFile(defaultDir)
                        // to handle the result(s)
                        .withChosenListener(new ChooserDialog.Result() {
                            @Override
                            public void onChoosePath(String path, File pathFile) {
                                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Locations", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("paths", path);
                                editor.apply();
                                Log.i("TAG",path);

                                String setting = sharedPreferences.getString("paths", "");

                                textPath = SettingsFragView.findViewById(R.id.txtPath);
                                textPath.setText(setting);

                            }
                        })
                        .build()
                        .show();
            }
        });


**This is my Download Manager!**


SharedPreferences sp = getActivity().getSharedPreferences("Locations", 0);
                                    String selectedImagePath = sp.getString("paths", "");

                                    //Toast.makeText(getContext(), model.getTitle() + " Downloading!.", Toast.LENGTH_SHORT).show();
                                    KToast.customColorToast(getActivity(), model.getTitle() + " Downloading!.", Gravity.BOTTOM, KToast.LENGTH_AUTO, R.color.Black, R.drawable.ic_app_logo_transparent);
                                    // Download Manager Request;
                                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl))
                                            // Set Title
                                            .setTitle(model.getTitle())
                                            // Set Description
                                            .setDescription(fileName)
                                            // Set Notification;
                                            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                                            // Allowed Network Methods;
                                            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                                            // Set Location to Download Files
//                                            .setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "/Electronic Releases/" + File.separator + fileName);
                                            .setDestinationInExternalFilesDir(selectedImagePath,File.separator + fileName);

0 个答案:

没有答案
相关问题