图像加载器不工作
Image loader is not working
我正在创建与本教程相同的项目 androidhive 之前它是有效的,但现在图像没有显示我启用的存储权限,我也收到这样的错误
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)
第 143 行错误
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();
}
}
}
这是我的代码,请有人帮助我
使用 Glide 或 Picasso 从 URL 加载图像。
对于 Glide,添加
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
然后:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://api.androidhive.info/images/sample.jpg").into(imageView);
或查看文档了解详情。 https://github.com/bumptech/glide。
对于Picasso,添加依赖项
compile 'com.squareup.picasso:picasso:2.5.2'
之后使用此代码从 URL 加载图像:
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView);
如果您想查看详细信息,请执行以下操作:http://square.github.io/picasso/
如果你想设置图像 而不使用任何第三方库,那么试试这个
try {
URL newurl = new URL(image_url);
Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
imageView.setImageBitmap(mIcon_val);
} catch (IOException e) {
e.printStackTrace();
}
或
使用 picasso 库
添加到您的 gradle 文件中
dependencies {
compile "com.squareup.picasso:picasso:2.4.0"
}
然后在 android menifest 文件中添加 internet 权限
<uses-permission android:name="android.permission.INTERNET" />
然后在 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);
}
}
你的问题是这样的:
Activity a = (Activity) photoToLoad.imageView.getContext();
您正在尝试从视图中获取 activity 的上下文。由于 appcompat-v7:24.0.0
图片的上下文不像以前那样工作,所以现在你不能那样做。
您有两个选择:降级或寻找其他方式将上下文传递给您的 class。
我正在创建与本教程相同的项目 androidhive 之前它是有效的,但现在图像没有显示我启用的存储权限,我也收到这样的错误
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)
第 143 行错误
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();
}
}
}
这是我的代码,请有人帮助我
使用 Glide 或 Picasso 从 URL 加载图像。
对于 Glide,添加
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
然后:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://api.androidhive.info/images/sample.jpg").into(imageView);
或查看文档了解详情。 https://github.com/bumptech/glide。
对于Picasso,添加依赖项
compile 'com.squareup.picasso:picasso:2.5.2'
之后使用此代码从 URL 加载图像:
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView);
如果您想查看详细信息,请执行以下操作:http://square.github.io/picasso/
如果你想设置图像 而不使用任何第三方库,那么试试这个
try {
URL newurl = new URL(image_url);
Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
imageView.setImageBitmap(mIcon_val);
} catch (IOException e) {
e.printStackTrace();
}
或
使用 picasso 库
添加到您的 gradle 文件中
dependencies {
compile "com.squareup.picasso:picasso:2.4.0"
}
然后在 android menifest 文件中添加 internet 权限
<uses-permission android:name="android.permission.INTERNET" />
然后在 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);
}
}
你的问题是这样的:
Activity a = (Activity) photoToLoad.imageView.getContext();
您正在尝试从视图中获取 activity 的上下文。由于 appcompat-v7:24.0.0
图片的上下文不像以前那样工作,所以现在你不能那样做。
您有两个选择:降级或寻找其他方式将上下文传递给您的 class。