Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save bitmap on internal storage download from internet

Tags:

android

I am using Asynctask to download a image from internet.

I want to save this image to internal storage and later I want to use this image.

I can download successfully but I cant find internal storage path where it is storing.

This DownloadImages.java

private class DownloadImages extends AsyncTask<String,Void,Bitmap> {

        private Bitmap DownloadImageBitmap(){
            HttpURLConnection connection    = null;
            InputStream is                  = null;

            try {
                URL get_url     = new URL("http://www.medyasef.com/wp-content/themes/medyasef/images/altlogo.png");
                connection      = (HttpURLConnection) get_url.openConnection();
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.connect();
                is              = new BufferedInputStream(connection.getInputStream());
                final Bitmap bitmap = BitmapFactory.decodeStream(is);
               // ??????????

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                connection.disconnect();
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return DownloadImageBitmap();
        }

    }

Any help will be appreciated. :)

Thank you.

like image 894
Olkunmustafa Avatar asked Dec 01 '22 17:12

Olkunmustafa


1 Answers

You can save and load the image on the internal storage like this: Saving:

public static void saveFile(Context context, Bitmap b, String picName){ 
    FileOutputStream fos; 
    try { 
        fos = context.openFileOutput(picName, Context.MODE_PRIVATE); 
        b.compress(Bitmap.CompressFormat.PNG, 100, fos);  
    }  
    catch (FileNotFoundException e) { 
        Log.d(TAG, "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d(TAG, "io exception"); 
        e.printStackTrace(); 
    } finally {
        fos.close();
    }
}

Loading:

public static Bitmap loadBitmap(Context context, String picName){ 
    Bitmap b = null; 
    FileInputStream fis; 
    try { 
        fis = context.openFileInput(picName); 
        b = BitmapFactory.decodeStream(fis);   
    }  
    catch (FileNotFoundException e) { 
        Log.d(TAG, "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d(TAG, "io exception"); 
        e.printStackTrace(); 
    } finally {
        fis.close();
    }
    return b; 
} 

But you will need to save the imageName somehow if you want to find it again if the app is closed. I would recomend an SQLLite database that map imageNames to entries in the database.

like image 68
Joakim Palmkvist Avatar answered Dec 10 '22 12:12

Joakim Palmkvist