Search code examples
androidthumbnailsandroid-sdcardandroid-gallery

save images to sdcard with thumbnails and pass it to a gridview without using the Gallery content provider?


I am making an app which downloads images from the web and stores them in sdcard before displaying them in a grid view. After googling a long time, i saw that it s possible to use the built-in gallery app content provider, wich generates thumbnails while storing images

MediaStore.Images.Media.insertImage(context.getContentResolver(),
                filename.getAbsolutePath(), filename.getName(),
                filename.getName()); 

I tried to use it, but i found that images are duplicated in the "DCIM/" folder (for me) and create thumbnails inside the "DCIM/.thumbnails/". Besides that, when i open the gallery app, i see my downloaded images !

My questions are:
- do i have to use the gallery app content provider, if yes how can i customize the folder source of thumbnails and images
- otherwise, how can i generate thumbnails (mini-kind, micro-kind as the gallery app does)


Solution

  • public Bitmap downloadImage() {
        URL myFileUrl = null;
        Bitmap bmImg = null;
        try {
            myFileUrl = new URL(this.getImageUrl());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            Log.i("im connected", "Download");
            bmImg = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return bmImg;
    }
    
    public void saveImage(Context context, Bitmap bmImg) {
        File filename;
    
        String imagePath = IMAGE_DIR;
        try {
            createDir(IMAGE_DIR);
    
            filename = new File(imagePath, this.getImage());
    
            FileOutputStream out = new FileOutputStream(filename);
    
            bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
    
            generateThumb(bmImg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
    public Bitmap generateThumb(Bitmap bitmap) {
        File filethumb;
        Bitmap thumb = null;
    
        String thumbPath = THUMB_DIR;
        try {
            createDir(THUMB_DIR);
    
            filethumb = new File(thumbPath, this.getImage());
    
            FileOutputStream out2 = new FileOutputStream(filethumb);
    
            thumb = Bitmap.createScaledBitmap(bitmap, 250, 340, false);
            thumb.compress(Bitmap.CompressFormat.JPEG, 90, out2);
    
            out2.flush();
            out2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thumb;
    }