Praveen Kumar
Praveen Kumar

Reputation: 225

download and store image from url

I want to download an image from the given url. the downloaded image should save in SD card. I have used the below code.

     URL newurl = null;
                    try {
                        newurl = new URL(strHitRes);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    try {
                        HttpURLConnection connection = (HttpURLConnection) newurl.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        Bitmap myBitmap = BitmapFactory.decodeStream(input);
                        String root = Environment.getExternalStorageDirectory().toString();
                        File myDir = new File(root + "/saved_images");
                        myDir.mkdirs();
                        Random generator = new Random();
                        int n = 10000;
                        n = generator.nextInt(n);
                        String fname = "Image-"+ n +".jpg";
                        File file = new File (myDir, fname);
                        if (file.exists ()) file.delete ();
                        try {
                            FileOutputStream out = new FileOutputStream(file);
                            myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                            Toast.makeText(getApplicationContext(),"download successful",Toast.LENGTH_LONG).show();
                            out.flush();
                            out.close();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

But image is not downloading. Even i tested in debug mode, i found that my bitmap is null. How to solve this.

Upvotes: 1

Views: 581

Answers (2)

AshisParajuli
AshisParajuli

Reputation: 689

//To download bitmap from URL

public Bitmap getbmpfromURL(String surl){
        try {
            URL url = new URL(surl);
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            urlcon.setDoInput(true);
            urlcon.connect();
            InputStream in = urlcon.getInputStream();
            Bitmap mIcon = BitmapFactory.decodeStream(in);
            return  mIcon;
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

To save bitmap to SD card

private void SaveImage(Bitmap finalBitmap) {

   String root = Environment.getExternalStorageDirectory().toString();
   File myDir = new File(root + "/saved_images");    
   myDir.mkdirs();
   Random generator = new Random();

   String fname = "Image.jpg";
   File file = new File (myDir, fname);
   if (file.exists ()) file.delete (); 
   try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

And don't forget to use below permission in your manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.INTERNET" /> 

Upvotes: 0

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

Say thanks to Vineet for his answer

try {
    URL url = new URL("url from apk file is to be downloaded");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, "filename.ext");

    FileOutputStream fileOutput = new FileOutputStream(file);
    InputStream inputStream = urlConnection.getInputStream();

    byte[] buffer = new byte[1024];
    int bufferLength = 0;

    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
        fileOutput.write(buffer, 0, bufferLength);
    }
    fileOutput.close();

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

Upvotes: 1

Related Questions