Not Able To Load Image From Server In Android
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 St
Solution 1:
Please try this functions get bitmap and download the bitmap
Bitmapbitmap= getBitmapfromUrl(imageurl);
imageview.setImageBitmap(bitmap);
SaveImage(bitmap);
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URLurl=newURL(imageUrl);
HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStreaminput= connection.getInputStream();
Bitmapbitmap= BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
returnnull;
}
}
privatevoidSaveImage(Bitmap finalBitmap) {
Stringroot= Environment.getExternalStorageDirectory().toString();
FilemyDir=newFile(root + "/saved_images");
myDir.mkdirs();
Randomgenerator=newRandom();
intn=10000;
n = generator.nextInt(n);
Stringfname="Image-" + n;
Filefile=newFile(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStreamout=newFileOutputStream(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
Solution 2:
Call this function from your activity and get inputStream. After getting inputStream (Bitmap bitmap = Bitmap.decodeStream(inputStream));
private InputStream OpenHttpConnection(String urlString)throws IOException
{
InputStreamin=null;
intresponse= -1;
URLurl=newURL(urlString);
URLConnectionconn= url.openConnection();
if (!(conn instanceof HttpURLConnection)) thrownewIOException("Not an HTTP connection");
try{
HttpURLConnectionhttpConn= (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)
{
thrownewIOException("Error connecting");
}
return in;
}
Solution 3:
Try using Glide
or Picasso
image processing library.
- 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);
- 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.
Post a Comment for "Not Able To Load Image From Server In Android"