当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Download Image From URL and show in Imageview

问题描述:

I want to show my downloaded image in my Imageview. I'm getting error " Type mismatch: cannot convert from ImageView to Bitmap " . Its saying I cannot show Bitmap images in imageview. Can anyone show me a way to accomplish it?

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 [email protected] 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 [email protected] 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)

网友答案:

You declared ImageView as

ImageView view;

You have to set Image on ImageView. It should be like this

view = (ImageView) findViewById(R.id.single_image);
view.setImageBitmap(bmImg);
网友答案:

I use Prime for all of my image loading. You can use the RemoteImageView class to make this really easy.

网友答案:

you can use asynk tast to do show:

  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;
}

and call this like:

new ImageDownloaderTask(thumbImage, item.thumbnail, context).execute(item.thumbnail);

分享给朋友:
您可能感兴趣的文章:
随机阅读: