我想在我的Imageview中显示下载的图像。我收到错误“类型不匹配:无法从ImageView转换为位图”。它说我无法在imageview中显示位图图像。谁能告诉我一个完成它的方法?
public class SingleImageViewActivity extends SherlockActivity {
// XML node keys
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_BIG_URL = "big_url";
private ProgressDialog pDialog;
String title;
String artist;
String big_image_url;
ImageView view;
URL url;
Bitmap bmImg = null;
ShareActionProvider mShareActionProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view_item);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
new loadSingleView().execute();
}
public class loadSingleView extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SingleImageViewActivity.this);
pDialog.setTitle("Connect to Server");
pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// updating UI from Background Thread
try {
Intent in = getIntent();
big_image_url = in.getStringExtra(KEY_BIG_URL);
title = in.getStringExtra(KEY_TITLE);
artist = in.getStringExtra(KEY_ARTIST);
url = new URL(big_image_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String args) {
// dismiss the dialog after getting all products
TextView lblName = (TextView) findViewById(R.id.name_title);
TextView lblCost = (TextView) findViewById(R.id.name_artist);
lblName.setText(title);
lblCost.setText(artist);
ActionBar ab = getSupportActionBar();
ab.setTitle(title);
ab.setSubtitle(artist);
view = (ImageView) findViewById(R.id.single_image);
view.setImageBitmap(bmImg);
pDialog.dismiss();
}
}
Logcat:
08-28 22:19:44.225: E/AndroidRuntime(20825): FATAL EXCEPTION: AsyncTask #3
08-28 22:19:44.225: E/AndroidRuntime(20825): java.lang.RuntimeException: An error occured while executing doInBackground()
08-28 22:19:44.225: E/AndroidRuntime(20825): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.lang.Thread.run(Thread.java:856)
08-28 22:19:44.225: E/AndroidRuntime(20825): Caused by: java.lang.NullPointerException
08-28 22:19:44.225: E/AndroidRuntime(20825): at com.example.androidhive.SingleImageViewActivity$loadSingleView.doInBackground(SingleImageViewActivity.java:91)
08-28 22:19:44.225: E/AndroidRuntime(20825): at com.example.androidhive.SingleImageViewActivity$loadSingleView.doInBackground(SingleImageViewActivity.java:1)
08-28 22:19:44.225: E/AndroidRuntime(20825): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-28 22:19:44.225: E/AndroidRuntime(20825): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-28 22:19:44.225: E/AndroidRuntime(20825): ... 4 more
08-28 22:19:49.631: E/WindowManager(20825): Activity com.example.androidhive.SingleImageViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c0328 that was originally added here
08-28 22:19:49.631: E/WindowManager(20825): android.view.WindowLeaked: Activity com.example.androidhive.SingleImageViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416c0328 that was originally added here
08-28 22:19:49.631: E/WindowManager(20825): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:428)
08-28 22:19:49.631: E/WindowManager(20825): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
08-28 22:19:49.631: E/WindowManager(20825): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
08-28 22:19:49.631: E/WindowManager(20825): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
08-28 22:19:49.631: E/WindowManager(20825): at android.view.Window$LocalWindowManager.addView(Window.java:537)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.Dialog.show(Dialog.java:278)
08-28 22:19:49.631: E/WindowManager(20825): at com.example.androidhive.SingleImageViewActivity$loadSingleView.onPreExecute(SingleImageViewActivity.java:72)
08-28 22:19:49.631: E/WindowManager(20825): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
08-28 22:19:49.631: E/WindowManager(20825): at android.os.AsyncTask.execute(AsyncTask.java:511)
08-28 22:19:49.631: E/WindowManager(20825): at com.example.androidhive.SingleImageViewActivity.onCreate(SingleImageViewActivity.java:57)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.Activity.performCreate(Activity.java:4465)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-28 22:19:49.631: E/WindowManager(20825): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 22:19:49.631: E/WindowManager(20825): at android.os.Looper.loop(Looper.java:137)
08-28 22:19:49.631: E/WindowManager(20825): at android.app.ActivityThread.main(ActivityThread.java:4512)
08-28 22:19:49.631: E/WindowManager(20825): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 22:19:49.631: E/WindowManager(20825): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 22:19:49.631: E/WindowManager(20825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-28 22:19:49.631: E/WindowManager(20825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-28 22:19:49.631: E/WindowManager(20825): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
我使用Prime来加载所有图片。您可以使用RemoteImageView
类来简化此操作。
答案 1 :(得分:2)
您将ImageView声明为
ImageView view;
您必须在ImageView上设置Image。 它应该是这样的
view = (ImageView) findViewById(R.id.single_image);
view.setImageBitmap(bmImg);
答案 2 :(得分:0)
您可以使用asynk tast
进行展示:
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private final MemoryCache memoryCache;
private final BrandItem brandCatogiriesItem;
private Context context;
private String url;
public ImageDownloaderTask(ImageView imageView, String url, Context context) {
imageViewReference = new WeakReference<ImageView>(imageView);
memoryCache = new MemoryCache();
brandCatogiriesItem = new BrandItem();
this.url = url;
this.context = context;
}
@Override
protected Bitmap doInBackground(String... params) {
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
memoryCache.put("1", bitmap);
brandCatogiriesItem.setUrl(url);
brandCatogiriesItem.setThumb(bitmap);
// BrandCatogiriesItem.saveLocalBrandOrCatogiries(context, brandCatogiriesItem);
imageView.setImageBitmap(bitmap);
} else {
Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
imageView.setImageDrawable(placeholder);
}
}
}
}
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
Log.d("URLCONNECTIONERROR", e.toString());
if (urlConnection != null) {
urlConnection.disconnect();
}
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
并将其称为:
new ImageDownloaderTask(thumbImage,item.thumbnail,context).execute(item.thumbnail);