Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open downloaded file in android with default availabe application in android

I am downloading a file from server on completion of download I have to open a file. Know the problem is the file could be of any type so I can't specify and Intent call to open a file with static name like we do to open a PDF file. What I want is when a file is downloaded it will search if any app is available to open the file else it will show pop up. I am doing all this inside fragment. Here my code for downloading :

public class DownloadFile extends AsyncTask<String, Void, Integer> {
    String file_name = "";
    File sdcard = Environment.getExternalStorageDirectory();
    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            HttpURLConnection url_conn = null;
            byte[] bffr;

            long totalSize = 0;
            File directory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/xyz/download");
            directory.mkdirs();
            // 06-03 17:57:41.160: D/file name(6882):
            file_name = "";
            file_name = params[0];
            Log.d("file name", file_name.toString());
            url_conn = (HttpURLConnection) (new URL("http://example.com/uploads/" + file_name)).openConnection();
            url_conn.setRequestMethod("GET");
            url_conn.setDoOutput(true);
            url_conn.connect();

            if (url_conn.getContentLength() > 0) {
                File imgFile = new File(sdcard + "/xyz/download/",file_name);
                FileOutputStream fos = new FileOutputStream(imgFile);
                InputStream is = url_conn.getInputStream();
                totalSize = url_conn.getContentLength();
                // Log.d("File Download Size ",totalSize+"");
                long total = 0;
                bffr = new byte[1024];
                int bufferLength = 0;
                while ((bufferLength = is.read(bffr)) > 0) {
                    total += bufferLength;
                    publishProgress("" + (int) ((total * 100) / totalSize));
                    fos.write(bffr, 0, bufferLength);
                }
                fos.close();
            } else
                Log.w(file_name.toString(), "FILE NOT FOUND");
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

    }

    private void publishProgress(String... process) {
        // TODO Auto-generated method stub
        mprogressDialog.setProgress(Integer.parseInt(process[0]));
    }

    protected void onPostExecute(Integer unused) {
        Log.d("after downloading file ", "file downloaded ");
        switch (unused) {
        case 0:
            mprogressDialog.dismiss();
             Intent install = new Intent(Intent.ACTION_VIEW);
                install.setDataAndType(Uri.fromFile(new File(sdcard + "/xyz/download/",file_name)),
                        "MIME-TYPE");
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               app.getBaseContext().startActivity(install);
            break;
        }
    }
}

In post execute I have tried to open it using Intent but that didn't worked. Any Idea is appreciated

like image 804
Neerav Shah Avatar asked Jul 13 '15 07:07

Neerav Shah


People also ask

How do I change open with on Android?

With the file selected, open the File menu then choose Get info. Go down to Open with and choose your preferred app from the list. Once that's done, click Change All and confirm your choice on the pop-up dialog.

Which app is used to open downloaded files?

Open the Android app drawer by swiping up from the bottom of the screen. Look for the My Files (or File Manager) icon and tap it. ... Inside the My Files app, tap "Downloads."


1 Answers

    File file = new File(filePath);
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
    String type = map.getMimeTypeFromExtension(ext);

    if (type == null)
        type = "*/*";

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.fromFile(file);

    intent.setDataAndType(data, type);

    startActivity(intent);
like image 172
Anoop M Maddasseri Avatar answered Oct 04 '22 22:10

Anoop M Maddasseri