15

I need to fetch images and some other data from server and then display it in the List. But as the number of records can be pretty large so I am not sure if I should save images to SQLite database or save it to SDCard or save those to memory.

Thanks, nil

nilMoBile
  • 1,932
  • 4
  • 15
  • 20
  • 2
    you can save path of images in SQlite database and can retrieve it easily. but if unfortunately you delete image which you stored already then you need to catch this. let me know if interested will give you sample code for it. – OnkarDhane Feb 14 '12 at 07:14
  • @nilMoBile you should check my [answer](http://stackoverflow.com/a/9141116/996493) it may help you :) – Lucifer Jun 01 '12 at 04:43
  • I came to this issue and I used to save image path to database for flexibility and for fast working, I recommended don't save binary values to database like images, Use internal memory to save images and used database to save images path. Answer given by Kurtis Nusbaum is well explained. – RobinHood Feb 14 '12 at 07:27
  • @OnkarDhane I am working on the same thing here. I appreciate if you can share sample code for this project to me. As i want to store path of image into database(don't want to use blob) and i'm not getting further. – Krups Apr 28 '16 at 08:12

5 Answers5

18

Always make a habit of saving images path to database. For a list view, be sure just to use those image's thumbnail. This will help you in fast loading of these images in list.

 long selectedImageUri = ContentUris.parseId(Uri.parse(anniEntry.getUri()));
 Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
                    mContext.getContentResolver(), selectedImageUri,MediaStore.Images.Thumbnails.MICRO_KIND,
                    null );

Here anniEntry.getUri() is the image uri.Now,put it in second code.U can get micro or mini thumbnail according to requirement

Shahzad Imam
  • 2,030
  • 2
  • 26
  • 45
  • that seems an excellent way to go with, do you have any sample to use the thumbnails way you mentioned. – nilMoBile Feb 14 '12 at 07:20
  • You have to use the uri to get Bitmap thumbnail.I am posting the code in edit.I you are satisfied with answer then accept it – Shahzad Imam Feb 14 '12 at 07:24
  • 1
    I think this is your another issues, so its better to search first and if you don't get any solution then again raise question on SO.@nilMoBile – RobinHood Feb 14 '12 at 07:29
12

It's generally considered bad form to save binary data like an image in a database. In most cases, for some technical reasons, it will actually end up damaging the performance of your database. Instead, save the images you want to cache to the SD card and just store the filepath of those images in the database.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • 1
    what if user delete the images of my application ? – Arun kumar Dec 23 '15 at 08:46
  • 2
    If the images are in internal storage it's only accessible by your app (which means the user can't delete them). If you store them on the SD card then you need to write your code such that it handles the case where the user deletes your image. Check out this for how to use internal storage: http://developer.android.com/training/basics/data-storage/files.html – Kurtis Nusbaum Dec 26 '15 at 00:47
3

If you have to download images & save in SDcard.Kindly follow below code:

package com.tablet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.UnknownHostException;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/*
 * This class download & store the media in external storage 
 * */
public class DownloadActivity extends Activity {

    private static String fileName = "android.jgp";

    private Button btnDownload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnDownload = (Button) findViewById(R.id.download);
        btnDownload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                download();
            }
        });


    private void download()
    {
        System.out.println("Inside Download");
        try {
            // this is the file you want to download from the remote server
            String path = "http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg";

            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/data/Kamal"
                //Environment.getExternalStorageDirectory()
            // + "/download/";
                    + "/";

            Log.v("log_tag", "PATH: " + PATH);
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, fileName);

            FileOutputStream f = new FileOutputStream(outputFile);
            InputStream in = c.getInputStream();
            Log.v("log_tag"," InputStream consist of : "+in.read());
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                //System.out.println("Reading byte : "+in.read(buffer));
                f.write(buffer, 0, len1);
            }
            Toast.makeText(this, "Download Completed Successfully @ "+PATH, Toast.LENGTH_LONG).show();
            f.close();


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "MalformedURLException", Toast.LENGTH_LONG).show();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "ProtocolException", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG).show();
        }catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "UnknownHostException", Toast.LENGTH_LONG).show();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "IOException", Toast.LENGTH_LONG).show();
        }
    }



}
Kamal Trivedi
  • 239
  • 3
  • 10
1

It a matter of application requirement and its implementation. I would suggest to use internal or sd card to store the images and save there path in db

Relsell
  • 771
  • 6
  • 18
  • what if user externally go to sd card and delete my application images ? – Arun kumar Dec 23 '15 at 08:45
  • 1
    Yes SD card storage is shared among application so it might be the case you can delete your application images , so you can always save images to private memory for application.. As per the original question is concerned bitmap cache can also be used as add on to enhance the user experience. – Relsell Dec 24 '15 at 17:27
1

Storing any heavy data that does not need the behavior of returning sub-parts of that data based on the query applied(like images, videos), should not be stored in database, rather only a reference to that data should be stored.

Nitin Bansal
  • 2,986
  • 3
  • 23
  • 30