Ganesh P Ganesh P - 5 months ago 30
Android Question

Image loader is not working

I am creating project same like this tutorial androidhive previously it was working, but now the image is not showing me enabled storage permission also I am getting error like this

java.lang.ClassCastException: android.support.v7.widget.TintContextWrapper cannot be cast to android.app.Activity
at com.example.admin.imageloading.ImageLoader$PhotosLoader.run(ImageLoader.java:143)


line no 143 error

class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}

@Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
try {
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}catch (Exception e){
e.printStackTrace();
}

}
}


this is my code please some one help me

Answer

If you want to set image without using any 3rd party libs so try this

try { 
    URL newurl = new URL(image_url); 
    Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
    imageView.setImageBitmap(mIcon_val);
} catch (IOException e) { 
e.printStackTrace(); 
}

OR

use picasso library

add in your gradle file

dependencies {

   compile "com.squareup.picasso:picasso:2.4.0"

}

Then add internet permission in android menifest file

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

Then add this below code in AndroidLoadImageFromURLActivity

  public class AndroidLoadImageFromURLActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // Loader image - will be shown before loading image
            int loader = R.drawable.loader;

            // Imageview to show
            ImageView image = (ImageView) findViewById(R.id.image);

            // Image url
            String image_url = "http://api.androidhive.info/images/sample.jpg";

          //Loading image from below url into imageView

         Picasso.with(AndroidLoadImageFromURLActivity.this)
                .load(url)
                .into(image);

        }
    }
Comments