Search code examples
androidandroid-download-managerdownload-manager

Android: How to refer to the file downloaded by DownloadManager


I have used the Download Manager Class to download a text file, the code that I download the file is:

private long enqueue
private DownloadManager dm;
String server_ip = "http://192.168.0.1/";

Request request = new Request(Uri.parse(server_ip + "test.txt"));
// Store to common external storage:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.txt");
enqueue = dm.enqueue(request);

And I have a boardcast receiver to check if the download is successful. If the download is successful, I will try to display the txt file in a textView:

BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

            Query query = new Query();
            query.setFilterById(enqueue);
            Cursor c = dm.query(query);

            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);

                // Check if the download is successful
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                    // Get the downloaded file and display the test.txt in a textView
                    File file = new File(storage_directory,"test.txt");
                    StringBuilder text = new StringBuilder();
                    try {
                        BufferedReader br = new BufferedReader(new FileReader(file));
                        String line;

                        while ((line = br.readLine()) != null) {
                            text.append(line);
                            text.append('\n');
                        }
                        br.close();

                        TextView tv = (TextView)findViewById(R.id.textView);
                        tv.setText(text);
                    }
                    catch (Exception e) {
                        //You'll need to add proper error handling here
                    }
                }
            }
        }
    }
}

One problem I found is if there is already a file with the same file name, "text.txt", the device will rename the newly download file to "text-1.txt". As a result when I try to display the newly downloaded file, it will display the old "test.txt" file instead. I would like to ask how can I refer to the new file when the download is successful, instead of specify a file name like what I did:

File file = new File(storage_directory,"test.txt");

Also, I have downloadeded the file to the external storage. I know if I did not add this line:

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.txt");

when I send the request to the download manager, the file will be donwloaded to an internal storage. How can I refer to the file in this case?

Thank you very much.

Update: If I add this line after received the file successfully in the broadcast receiver:

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

The uriString give me

file:///storage/emulated/0/Download/test.txt


Solution

  • Having many attempts, I found a way to solve my problem. I do not know if this is a good solution, but it seems work. I added the following code after it received the file successfully in the broadcast receiver, that is I added:

    int filenameIndex = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
    String filename = c.getString(filenameIndex);
    File file = new File(filename);
    

    and removed this code:

    File file = new File(storage_directory,"test.txt");
    

    after:

    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
    

    By doing so, it will refer to the new downloaded file even the system renamed the file.