Emilio Gaines
Emilio Gaines

Reputation: 95

Bitmap error with imageview

I am creating an app that is downloading a file then loads it onto an imageview but I am getting this error

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

and this is my download code

   class DownloadFileFromURL extends AsyncTask<String, String, String> {


        // Show Progress bar before downloading Music
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Shows Progress Bar Dialog and then call doInBackground method
            showDialog(progress_bar_type);
        }

        // Download File from Internet
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                // Get Music file length
                int lenghtOfFile = conection.getContentLength();
                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(),10*1024);
                // Output stream to write file in SD card
                int imageNr = sharedPreferences.getInt("ImageNr", 1);
                OutputStream output = new FileOutputStream(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg");

                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    output.write(data, 0, count);
                    total += count;
                    // Publish the progress which triggers onProgressUpdate method
                    publishProgress("" + (total));

                    // Write data to file

                }
                // Flush output
                output.flush();
                // Close streams
                output.close();
                input.close();
            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }
            return null;
        }
        // While Downloading Music File
        protected void onProgressUpdate(String... progress) {
            // Set progress percentage
            prgDialog.setProgress(Integer.parseInt(progress[0]));
        }

        // Once File is downloaded
        @Override
        protected void onPostExecute(String file_url) {
            // Dismiss the dialog after the Music file was downloaded
            dismissDialog(progress_bar_type);

            int imageNr = sharedPreferences.getInt("ImageNr", 1);
            File imgFile = new File(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg");
            imageNr++;
            SharedPreferences.Editor editorsave = sharedPreferences.edit();
            editorsave.putInt("ImageNr", imageNr);
            editorsave.apply();

            if(imgFile.exists()){
                Toast.makeText(getApplicationContext(), "Bild laddad!", Toast.LENGTH_LONG).show();

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

                imageView.setImageBitmap(myBitmap);

            }else{
                Toast.makeText(getApplicationContext(), "Bild ej hittad!", Toast.LENGTH_LONG).show();
            }
            // Do stuff here

        }
    }

yeah it uses some deprecated code but this is working for me in my other application, though I am not using bitmap in that one but the AsyncTask works.

Upvotes: 0

Views: 1273

Answers (1)

Dimitar Genov
Dimitar Genov

Reputation: 2066

your imageViewis null

  • to stay way from the exception itself

    if(imgFile.exists() && imageView != null) {
    ....
    
  • check your imageView reference assignment. It seems imageView reference is not assigned properly, hence returns null

Upvotes: 1

Related Questions