Android Save images to SQLite or SDCard or memory

前端 未结 5 957
小蘑菇
小蘑菇 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();
            }
        }
    
    
    
    }
    

提交回复
热议问题