mardi 2 février 2016

Approach to Android images in DB

It's an App with kind of gallery context. Using REST I am receiving (Volley) JSON which contains information about place and few links to the pictures. Because there is about 60 places, so the caching form PICASSO is not enough. Data is stored in DB so I my idea was to insert an image to the database as byte stream. It's done, but in not so good way, presented below. Code inside AsyncTask.

@Override
        protected Bitmap doInBackground(Void... params) {
            try {
                return Picasso.with(context)
                        .load(site.getImageURL())
                        .get();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            } catch (Exception e) {
                e.printStackTrace();
            }
            site.setImageBytes(stream.toByteArray());

            db.openForWrite();
            db.updateSite(site.getId(), site);
            db.close();
        }

My question is how to make it efficient, do you know better way to do that? I tried to callback to Picasso to add image to the DB onSuccess, but it require the view as a first argument.

Maybe there is a good ways of saving images to SD card, and replacing HTTP link with location of image on SD card.

Or caching image to the disc directly?

Aucun commentaire:

Enregistrer un commentaire