Search code examples
androidfileloggingstorageinternal

Error downloading file on internal storage android


As I was thinking that downloading is pretty straightforward I am encountering a error when I try to download an xml file from the internet and save it on the internal memory. I use the internal memory because I am testing my app on samsung galaxy s3.

Here's the code for the downloader. It's actually a thread which is invoked from the activity

public class DownloaderThread extends Thread
{
    private static final int DOWNLOAD_BUFFER_SIZE = 4096;
    Context context;
    private String downloadUrl = "some address";

    public void run()
    {
            URL url;
            URLConnection conn;
            String fileName;
            BufferedInputStream inStream;
            BufferedOutputStream outStream;
            File outFile;
            FileOutputStream fileStream;

            try
            {
                    url = new URL(downloadUrl);
                    conn = url.openConnection();
                    conn.setUseCaches(false);
                    fileName = "file.xml";

                    Log.v("XML", "Download Started");

                    // start download
                    inStream = new BufferedInputStream(conn.getInputStream());
                    outFile = new File(context.getFilesDir() + "/" + fileName);
                    fileStream = new FileOutputStream(outFile);
                    outStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
                    byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
                    int bytesRead = 0;
                    while(!isInterrupted() && (bytesRead = inStream.read(data, 0, data.length)) >= 0)
                    {
                            outStream.write(data, 0, bytesRead);
                    }

                    outStream.close();
                    fileStream.close();
                    inStream.close();

                    if(isInterrupted())
                    {
                            outFile.delete();
                    }
                    else
                    {
                            Log.v("XML","Download Finished");
                            Log.v("PATH","File is stored in direcotry:" + outFile.getAbsolutePath().toString());
                    }
            }
            catch(MalformedURLException e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());

            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Log.v("Error",e.toString());
            }
    }

and here is the code which invokes the thread :

public boolean onOptionsItemSelected(MenuItem item) {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
     android.net.NetworkInfo datac = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     if ( item.getItemId() == DOWNLOAD_XML_MENU_ITEM ) {
            if ((wifi != null & datac != null) && (wifi.isConnected() | datac.isConnected())) 
            {
                downloaderThread = new DownloaderThread();
                downloaderThread.start();
            }
            else 
            {
                displayConnectionAlert();
            }
        }
        return true;
    }

the errors I receive are: tag trace - no such file and directory and after log downloaded started i get NullPointerException on the path of the outFile. Any ideas why?


Solution

  • You're using a Context reference that is never assigned a value, hence the NPE.

    Context context; // never initialised anywhere, thus null
    

    Since all the variable is used for is to resolve context.getFilesDir(), I suggest you simply create a constructor for your thread class that takes the path as an argument; i.e.:

    public DownloaderThread(String targetDir) { /* implement here */ }
    

    Alternatively you can actually pass in a Context instance, but just make sure it's the application context (rather than an Activity) to avoid potential memory leaks.