Josh Parinussa
Josh Parinussa

Reputation: 643

How to fix DownloadManager can't open file and file missing?

I have project using downloadManager, i already can show the download progress in screen, but the problem is when after i download the file then click the download complete notif it said that "Can't open file" This is my code,

public class MainActivity extends AppCompatActivity {
    Button tombolDownload;
    DownloadManager downloadManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tombolDownload = (Button) findViewById(R.id.btnDownload);
        tombolDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
                Uri uri = Uri.parse("http://192.168.1.64/FileAPK/app1.apk");
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                Long reference = downloadManager.enqueue(request);
            }
        });
    }
}

How i can fix this problem?

Upvotes: 2

Views: 3102

Answers (2)

Eduardo Sztokbant
Eduardo Sztokbant

Reputation: 792

I had a similar issue with the download of a zip file.

Upon attempting to open the downloaded file by clicking on the Download Manager's notification, I would get the same Can't open file notification.

In my particular case, I learned that it had nothing to do with the code itself – as the file was indeed being properly downloaded – but with the fact that the device being used did not have an installed app which could handle zip files.

After I installed a 3rd-party app which was enabled to handle zip files, I wouldn't have this issue anymore, as the the downloaded file would now be opened by that app.

Upvotes: 0

Sahil
Sahil

Reputation: 1419

I think you should declare the MIME type also of APK file.

MIME type is : application/vnd.android.package-archive .

Use this as following:

request.setMimeType("application/vnd.android.package-archive");

Edit: Add the following line also so that it knows where to store it. In your code it was saving the file with some random number without extension, hence the can't open file problem was happening.

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"app1.apk");

Where app1.apk is name of file with extension and also add setMimeType()

dont forget the persmissions WRITE_EXTERNAL_STORAGE

hope it helps

Upvotes: 3

Related Questions