3

Here I am trying to download a image from server. But it is always throwing Exception. Can any One tell me Why it is Happening and what will be the Correct way?

 public static String getBitmap(String url) throws IOException {

        InputStream is = (InputStream) new URL(url).getContent();
        Bitmap bmp= BitmapFactory.decodeStream(is);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] b=stream.toByteArray();
        String encoded = Base64.encodeToString(b, Base64.DEFAULT);


        is.close();
        return encoded;


}
rashmi ranjan
  • 380
  • 1
  • 4
  • 14

3 Answers3

0

Please try this functions get bitmap and download the bitmap

Bitmap bitmap = getBitmapfromUrl(imageurl);
imageview.setImageBitmap(bitmap);

SaveImage(bitmap);

public Bitmap getBitmapfromUrl(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        return bitmap;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}

private void SaveImage(Bitmap finalBitmap) {

    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;
    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 Add this permision in your manifest file

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
0

Try using Glide or Picasso image processing library.

  1. here is Glide demo
Glide.with(this).load(YourImageURL)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                 .into(Imageview);

and add this dependency in gradle.

compile 'com.github.bumptech.glide:glide:3.7.0'

you can also set placeholder while image loading. its like alt attribute in html

Glide.with(this).load(YourImageURL)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.drawable.backimage)
                    .into(Imageview);
  1. Here is Picasso demo
   Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder)
        .error(R.drawable.user_placeholder_error)
        .into(imageView);

and add this dependency:

compile 'com.squareup.picasso:picasso:2.5.2'

these lib also provide cache feature so you don't need to load second time.

Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
  • `But it is always throwing Exception. Can any One tell me Why it is Happening and what will be the Correct way?` You did not answer the questions. Your answer is not an answer but an advise. – greenapps May 16 '17 at 08:22
0

Call this function from your activity and get inputStream. After getting inputStream (Bitmap bitmap = Bitmap.decodeStream(inputStream));

private InputStream OpenHttpConnection(String urlString) throws IOException
    {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");

    try{
    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestMethod("GET");
    httpConn.connect();

    response = httpConn.getResponseCode();
    if (response == HttpURLConnection.HTTP_OK) {
    in = httpConn.getInputStream();
    }
    }
    catch (Exception ex)
    {
    throw new IOException("Error connecting");
    }
    return in;
    }
nivesh shastri
  • 430
  • 2
  • 13