Downloaded files are missing after download in android

I'm using the following code to download and save files from server to the external storage, it is working well in some devices and the files are available in the external storage while in some devices the files are missing after download. How to resolve this issue? This code is working on Tab 3 (Model: SM-T311 OS: 4.4.2) while not working on Note 2 (Model No: GT-N7100 OS: 4.4.2)

public class DownloadLoadAttachementAsyncTask extends AsyncTask<String, String, String> { private String fileName; private ProgressDialog dialog; private String downloadUrl; private Context ctx; private String attachmentName; public DownloadLoadAttachementAsyncTask(String fileName, Context context) { this.fileName = fileName; this.attachmentName = fileName; this.ctx = context; } @Override protected void onPreExecute() { super.onPreExecute(); dialog = new ProgressDialog(ctx); dialog.setMessage(ctx.getString(R.string.download_attachements)); dialog.setCanceledOnTouchOutside(false); dialog.show(); downloadUrl = fileName; String[] parts = downloadUrl.split("attachmentName="); String part1 = parts[0]; // 004 fileName = parts[1]; // 034556 File f = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Attachments"); if (!f.exists()) f.mkdirs(); } @Override protected String doInBackground(String... params) { try { int count; URL url = new URL(downloadUrl); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(Environment .getExternalStorageDirectory().getAbsolutePath() + "/Attachments/" + fileName); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; publishProgress("" + (int) ((total * 100) / lenghtOfFile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) { } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); if (dialog.isShowing()) { dialog.dismiss(); String filename = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Attachments/" + fileName; File file = new File(filename); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/*"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); } } }

Category:android Views:5 Time:2018-12-11
Tags: android

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.130 (s). 11 q(s)