Skip to content Skip to sidebar Skip to footer

Download Image And Save In Gallery?

I am getting response from server and in my response i am getting image,the view of that response is in listview,now what i want is below image i have one button for download image

Solution 1:

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

classDownloadImageAsyncTaskextendsAsyncTask<Void, Void, Void> 
   {

            @OverrideprotectedVoiddoInBackground(Void... params)
            {
                // TODO Auto-generated method stubdownloadImages();
                returnnull;
            }

            @OverrideprotectedvoidonPostExecute(Void result)
            {
                // TODO Auto-generated method stubsuper.onPostExecute(result);

            }

    }

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

privatevoiddownloadImages()
    {

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

            HttpURLConnection urlConnection;
            try
            {
                urlConnection = (HttpURLConnection) imageUrl.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();
                Stringpath= getExternalCacheDir() + File.separator + getResources().getString(R.string.app_name);
                Filef=newFile(path);
                if (!f.exists())
                {
                    f.mkdirs();
                }
                Filefile=newFile(f, "ImageName.png"); // Here you can save the image with name and extensionif (!file.exists())
                {
                    file.createNewFile();
                }
                FileOutputStreamfileOutput=newFileOutputStream(file);
                InputStreaminputStream= urlConnection.getInputStream();
                byte[] buffer = newbyte[1024];
                intbufferLength=0; // used to store a temporary size of the// bufferwhile ((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.

Solution 2:

use this code for to download image from URL

public Bitmap getBitmapFromURL(String src) {
   try {
       java.net.URLurl=newjava.net.URL(src);
       HttpURLConnectionconnection= (HttpURLConnection) url
            .openConnection();
       connection.setDoInput(true);
       connection.connect();
       InputStreaminput= connection.getInputStream();
       BitmapmyBitmap= BitmapFactory.decodeStream(input);
       return myBitmap;
   } catch (IOException e) {
       e.printStackTrace();
       returnnull;
   }
}

Solution 3:

Use the following code to download the image from url:

privatestatic Bitmap getBitmapFromUrl(String imgURL)throws IOException {

            URLIMGURL=newURL(imgURL);           
            HttpURLConnectionconn= (HttpURLConnection) IMGURL.openConnection();
            conn.setDoInput(true);
            conn.connect();

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

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

            return bmp;
        }

It will return a bitmap ready to save on storage.

Post a Comment for "Download Image And Save In Gallery?"