0

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)

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52

2 Answers2

1
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;
}
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
-1
Bitmap thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filename.getAbsolutePath()), 80, 80);
L.Grillo
  • 960
  • 3
  • 12
  • 26