Load Multiple Url Images Without Using Third Party Libraries In Recyclerview
Hi all I'm new to Android, I'm facing difficulty in loading Multiple images through URL into a Recycler view, My task is not to use any third Party Libraries and also not to add in
Solution 1:
@sample AsycTask Code, you can pass the url to this class by execute method.
publicclassShowImageextendsAsyncTask<String,Void,Bitmap>{
private WeakReference<ImageView> imageview;
publicShowImage(ImageView imv){
imageview=new WeakReference<ImageView>(imv);
}
/** Background process
* input:url
* output: Bitmap image
* It passed into onPostExecute method
**/
@Override
protected Bitmap doInBackground(String... urls) {
return getBitMapFromUrl(urls[0]);
}
/** This method called after the doINputBackground method
* input:Bitmap image
* output: image set into the image view
* Image view passed from RecyclerViewOperation to ShowImage class through constructor
**/
@Override
protectedvoidonPostExecute(Bitmap result) {
if((imageview!=null)&&(result!=null)){
ImageView imgview=imageview.get();
if(imgview!=null){
imgview.setImageBitmap(result);
}
}
}
/** This method called by doInBackground method
* input:url
* output: Bitmap image
*
**/private Bitmap getBitMapFromUrl( String imageuri){
HttpURLConnection connection=null;
try {
URL url=new URL(imageuri);
// Log.d("bucky","bitmap" + imageuri);
connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream();
Bitmap mybitmap=BitmapFactory.decodeStream(is);
return mybitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
returnnull;
} catch (IOException e) {
e.printStackTrace();
returnnull;
}
finally {
if(connection!=null) {
connection.disconnect();
}
}
}
}
Solution 2:
You can use AsynTask so it will load multiple Images from the Url, which provides the functionality to work in Background. So Your main thread does not gets affected and the Images downloads in the backgreound continously. I hope this answer your Question.
Post a Comment for "Load Multiple Url Images Without Using Third Party Libraries In Recyclerview"