download image and save in gallery?

守給你的承諾、 提交于 2019-12-01 11:45:27

I did it by using AsyncTask to download image from a URL.

  class DownloadImageAsyncTask extends AsyncTask<Void, Void, Void> 
   {

            @Override
            protected Void doInBackground(Void... params)
            {
                // TODO Auto-generated method stub
                downloadImages();
                return null;
            }

            @Override
            protected void onPostExecute(Void result)
            {
                // TODO Auto-generated method stub
                super.onPostExecute(result);

            }

    }

This is the download image function which you can get reference from many sites.

private void downloadImages()
    {

        URL imageUrl; //your URL from which image to be downloaded
        String domain;
        try
        {
            imageUrl = new URL("your URL");

            HttpURLConnection urlConnection;
            try
            {
                urlConnection = (HttpURLConnection) imageUrl.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();
                String path = getExternalCacheDir() + File.separator + getResources().getString(R.string.app_name);
                File f = new File(path);
                if (!f.exists())
                {
                    f.mkdirs();
                }
                File file = new File(f, "ImageName.png"); // Here you can save the image with name and extension
                if (!file.exists())
                {
                    file.createNewFile();
                }
                FileOutputStream fileOutput = new FileOutputStream(file);
                InputStream inputStream = urlConnection.getInputStream();
                byte[] buffer = new byte[1024];
                int bufferLength = 0; // used to store a temporary size of the
                                        // buffer
                while ((bufferLength = inputStream.read(buffer)) > 0)
                {
                    fileOutput.write(buffer, 0, bufferLength);
                }
                fileOutput.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }

    }

Then in your Adapter class you can set the image by doing this:

holder.propic.setImageDrawable(Drawable.createFromPath(getExternalCacheDir() + File.separator
    + getResources().getString(R.string.app_name) + File.separator + "ImageName.png"));  //Exact path where the image is saved.

Hope it might help you.

use this code for to download image from URL

public Bitmap getBitmapFromURL(String src) {
   try {
       java.net.URL url = new java.net.URL(src);
       HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
       connection.setDoInput(true);
       connection.connect();
       InputStream input = connection.getInputStream();
       Bitmap myBitmap = BitmapFactory.decodeStream(input);
       return myBitmap;
   } catch (IOException e) {
       e.printStackTrace();
       return null;
   }
}

Use the following code to download the image from url:

private static Bitmap getBitmapFromUrl(String imgURL)
                throws IOException {

            URL IMGURL = new URL(imgURL);           
            HttpURLConnection conn = (HttpURLConnection) IMGURL.openConnection();
            conn.setDoInput(true);
            conn.connect();

            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);

            InputStream is = conn.getInputStream();
            bmp = BitmapFactory.decodeStream(is);

            return bmp;
        }

It will return a bitmap ready to save on storage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!