Abhishek Sabbarwal
Abhishek Sabbarwal

Reputation: 3808

Downloaded file from Android App not visible in Downloads directory

I am in process of developing an app wherein I am downloading pdf files from a remote server. I am so far successful in downloading the PDF files on my phone via the app. The problem that I am facing is that the downloaded file is not visible in the Downloads directory on my Galaxy Nexus. When I use the file manager app I can see the file there and it opens up real nice.

I tried using the following options in my code but none of them seems to solve my problem (both these options successfully download the file and its visible in the file manager) :

outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);

And

outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

Can someone please help me with some clues ? Any hint or clue will be of great help.

Upvotes: 3

Views: 4485

Answers (1)

Abhishek Sabbarwal
Abhishek Sabbarwal

Reputation: 3808

I was able to do it using the DownloadManager in the following way :

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(
            Uri.parse("http://"));
    enqueue = dm.enqueue(request);

     BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {
                            Toast.makeText(getApplicationContext(), "Download Complete!!!", Toast.LENGTH_LONG).show();

                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));

Upvotes: 2

Related Questions