Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to download a PDF file from my app,but it's not working
    primarykey
    data
    text
    <p>I am beginner in android development,I am working on a project and for that i need to download the PDF file from a URL to my phone using <strong>async task</strong>,also i need to show the <strong>progress dialog</strong> while the file is being downloaded.</p> <p><strong>Here's how my MainActivity.java goes..</strong></p> <pre><code>public class MainActivity extends Activity { String[] listItem; String[] listItemURLs; ProgressDialog mProgressDialog; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.diff_view1); // instantiate it within the onCreate method mProgressDialog = new ProgressDialog(MainActivity.this); mProgressDialog.setMessage("Downloading file..please wait."); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); listItem = new String[] { "File 1", "File 2", "File 3", "File 4", "File 5", "File 6", "File 7" }; listItemURLs = new String[] { "http://www.ztsinc.com/MBTLA2_ds.pdf", "http://www.ztsinc.com/MBT1_ds.pdf", "http://www.ztsinc.com/MINIMBT_ds.pdf", "http://www.ztsinc.com/MINI9R_ds.pdf", "http://www.ztsinc.com/MBTLA2_OI.pdf", "http://www.ztsinc.com/MBT1_OI.pdf", "http://www.ztsinc.com/MINIMBT_MINI9R_OI.pdf" }; lv = (ListView) findViewById(R.id.listview1); lv.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_list_item_1, listItem)); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, final View view, final int position, long id) { AlertDialog.Builder da = new AlertDialog.Builder( MainActivity.this); da.setMessage("Do you want to download this file?"); da.setPositiveButton("YES", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { downloadPDF downloadpdf = new downloadPDF(view, position); downloadpdf.execute(listItemURLs[position]); } }); da.setNegativeButton("No", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); da.show(); } }); } public class downloadPDF extends AsyncTask&lt;String, Integer, String&gt; { private View mView; private int mPosition; public downloadPDF(View view, int position) { mView = view; mPosition = position; } @Override public void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } String fileName = listItem[mPosition]; String fileExtension=".pdf"; @Override protected String doInBackground(String... sUrl) { try { URL url = new URL(sUrl[mPosition]); HttpURLConnection connection =(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); String PATH = Environment.getExternalStorageDirectory() + "/download/"; File file = new File(PATH); file.mkdirs(); File outputFile = new File(file,fileName + fileExtension); FileOutputStream fos = new FileOutputStream(outputFile); InputStream is = connection.getInputStream(); // this will be useful so that you can show a typical 0-100% // progress bar int fileLength = connection.getContentLength(); // download the file byte buffer[] = new byte[1024]; long total = 0; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { total += len1; // publishing the progress.... publishProgress((int) (total * 100 / fileLength)); fos.write(buffer, 0, len1); } fos.flush(); fos.close(); is.close(); } catch (Exception e) { } return null; } public void onPostExecute(Integer... progress) { super.onProgressUpdate(progress); mProgressDialog.setProgress(progress[0]); Toast.makeText(MainActivity.this, "Download Complete", Toast.LENGTH_SHORT).show(); mProgressDialog.dismiss(); } } } </code></pre> <p><strong>My diff_view1.xml goes like this...</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1" &gt; &lt;ListView android:id="@+id/listview1" android:layout_width="fill_parent" android:layout_height="400dp" &gt; &lt;/ListView&gt; &lt;Button android:id="@+id/btnDownload" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Download" android:layout_marginTop="30dp"/&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>I have also added permissions in my manifest for internet,sdcard write,etc..</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.testProject.imageCheck" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".MainActivity" android:label="Titles/Styled (via theme)" android:theme="@style/StyledIndicators" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>I have a listview filled up with an array adapter with elements like "File 1","File 2",etc. I am initiating object of the async task with "URLs" (those stored in array) as arguement,but when i run the and click on any of the listview item,the progress bar just gets stuck at 0% and then nothing happens. Can anyone help me? been doing this for hours but no success:(</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload