Android Save images to SQLite or SDCard or memory

前端 未结 5 941
小蘑菇
小蘑菇 2020-11-29 05:58

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 im

相关标签:
5条回答
  • 2020-11-29 06:36

    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();
            }
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-29 06:36

    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

    0 讨论(0)
  • 2020-11-29 06:39

    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

    0 讨论(0)
  • 2020-11-29 06:39

    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.

    0 讨论(0)
  • 2020-11-29 06:43

    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.

    0 讨论(0)
提交回复
热议问题