0

Good morning, I have following code to download bitmpa from server

/*SAVE IMAGE++++++++++++++++++++*/
    public void saveImage(Context context, Bitmap b, String imageName) {
        FileOutputStream foStream;
        try {
            foStream = context.openFileOutput(imageName, Context.MODE_PRIVATE);
            b.compress(Bitmap.CompressFormat.JPEG, 100, foStream);
            foStream.close();
            UPDTV_FOTO.setBackgroundColor(Color.GREEN);
            image.setImageBitmap(loadImageBitmap(getApplicationContext(), Giocatore));
            UPDTV_FOTO.setText("Download "+Giocatore+ " Complete");
        }  
        catch (FileNotFoundException e) { 
            Log.d("saveImage", "file not found"); 
            e.printStackTrace();
         //   UPDTV_FOTO.setText("FILE NOT FOUND");
        }  
        catch (IOException e) { 
            Log.d("saveImage", "io exception"); 
            e.printStackTrace();
          //  UPDTV_FOTO.setText("SAVE IMAGE ERROR");
        } 
    }
/*SAVE IMAGE+++++++++++++++++++++++++*/     


/*DOWNLOAD IMAGE++++++++++++++++++++++*/
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        private String TAG = "DownloadImage";
        private Bitmap downloadImageBitmap(String sUrl) {
            Bitmap bitmap = null;
            try {
                InputStream inputStream = new URL(sUrl).openStream();   // Download Image from URL
                bitmap = BitmapFactory.decodeStream(inputStream);       // Decode Bitmap
                inputStream.close();
            } catch (Exception e) {
                Log.d(TAG, "Exception 1, Something went wrong!");
                e.printStackTrace();
            //  UPDTV_FOTO.setText("DOWNLOAD ERROR");
            }
            return bitmap;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return downloadImageBitmap(params[0]);
        }

        protected void onPostExecute(Bitmap result) {
            saveImage(getApplicationContext(), result, Giocatore);
        }
    }
/*DOWNLOAD IMAGE++++++++++++++++++++++*/        


/*LOAD IMAGE*+++++++++++++++++++++++++++++++++++++*/
    public Bitmap loadImageBitmap(Context context, String imageName) {
        Bitmap bitmap = null;
        FileInputStream fiStream;
        try {
            fiStream    = context.openFileInput(imageName);
            bitmap      = BitmapFactory.decodeStream(fiStream);
            fiStream.close();
        } catch (Exception e) {
            Log.d("saveImage", "Exception 3, Something went wrong!");
            e.printStackTrace();
        //  UPDTV_FOTO.setText("LOAD ERROR");
        }
        return bitmap;
    }
    /*LOAD IMAGE+++++++++++++++++++++++++++*/

if the bitmap I try to download is available on server all works fine with

new DownloadImage().execute(myurl);

else if is not available, my app crashes. So I would like to check if bitmap is available on servere before starting download.

I try

if (URLUtil.isValidUrl(URL+FotoGiocatore)==true);

and also

How can I programmatically test an HTTP connection?

Check if file exists on remote server using its URL

lucignolo
  • 223
  • 2
  • 12

1 Answers1

0

You can also use below code to check if the image is present

URL url = new URL("YOUR URL");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");

urlConnection.connect();

`int status = urlConnection.getResponseCode();` 

and only start download if status==200

Kapil G
  • 4,081
  • 2
  • 20
  • 32
  • java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference – lucignolo Aug 07 '17 at 15:02
  • where did you receive this error? @lucignolo doesn't look in this code – Kapil G Aug 08 '17 at 13:15
  • solved adding if (b!=null) in public void saveImage. – lucignolo Aug 08 '17 at 15:19