我想在我的 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();
}
}
日志猫:
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)