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

I download the file from the server when the download is complete. I need to open a file. Know, the problem is that the file can be of any type, so I cannot specify an Intent call to open a file with a static name, as we do to open a PDF file. What I want is when the file is uploaded, it will search if any application is available to open the file, which will be displayed as a pop-up. I do all this inside a fragment. Here is my download code:

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 tried to open it with Intent, but that didn't work. Any idea appreciated

+5
source share
4 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); 

See this one too.

+5
source
  install.setDataAndType(Uri.fromFile(new File(sdcard + "/xyz/download/",file_name)), "MIME-TYPE"); 

You must install MIME-TYPE according to the type of file and open it in the available applications on the device. Contact fooobar.com/questions/130132 / ...

0
source

I think you need to pass a valid mime type to get the application selection popup. you can get mimeType from the file name or file instance.

 String fileName = "/path/to/file"; MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); // only by file name String mimeType = mimeTypesMap.getContentType(fileName); 

source fooobar.com/questions/19050 / ...

0
source

After loading do this -

  MimeTypeMap myMime = MimeTypeMap.getSingleton(); Intent newIntent = new Intent(Intent.ACTION_VIEW); String mimeType = myMime.getMimeTypeFromExtension(fileExt(path).substring(1)); newIntent.setDataAndType(Uri.fromFile(new File(path)), mimeType); newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { ReadMailActivity.this.startActivity(newIntent); } catch (ActivityNotFoundException e) { Toast.makeText(ReadMailActivity.this, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } 
0
source

All Articles