-3

I have already referred this link How to store files generated from app in "Downloads" folder of Android? And I am facing the same issue as mentioned in the Update1. I want the files to be saved in Download folder which is be seen directly from Home/Menu screen but it is saving in /storage/emulated/0/download My code:

File downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        final String fileName = "test.pdf";
        final File file = new File(downloadDir, fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                            bos.write(response);
                            bos.flush();
                            bos.close();
Rohit
  • 23
  • 10
  • Why are you giving negative rating. It is a problem and many of us has tried it today with no luck. That why we finally posted it. – Rohit Jun 14 '17 at 10:11

1 Answers1

0

instead of your first line try this :

String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Downloads";
File dir = new File(dirPath);

see if that helps.

SepJaPro2.4
  • 704
  • 9
  • 19
  • Did not work. Got following error:java.io.FileNotFoundException: /storage/emulated/0/storage/emulated/0/Downloads/test.pdf(No such file or directory) – Rohit Jun 14 '17 at 08:18
  • you can get rid of this error by adding `if (!dir.exists()) dir.mkdirs();` – SepJaPro2.4 Jun 14 '17 at 09:10
  • but i think your first solution `File downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);` was giving you the correct downloads folder path and the download folder you are mentioning which is on the menu depends on the device you are using as test device and not every device has that in menu. – SepJaPro2.4 Jun 14 '17 at 09:14
  • That download folder already exists so I don't have to create it. I am not sure but I think by default new versions of os already has that folder, it may not depend on manufacturer. However I am not sure about it. But my problem remains the same. By using the mentioned method I am able to save in /storage/emulated/0/storage/emulated/0/Downloads/ which is not I want. – Rohit Jun 14 '17 at 10:07
  • sorry that i couldn't be of any help. maybe it helps you best that you search by that path considering the manufacturer, because i've been using Samsung devices for many years and the one i have right now also has Android N on it but it doesn't have such folder. – SepJaPro2.4 Jun 14 '17 at 11:10