3

ダウンロードした画像を 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)
4

3 に答える 3

3

私はすべての画像の読み込みにPrimeを使用しています。クラスを使用するRemoteImageViewと、これを非常に簡単に行うことができます。

于 2012-08-28T14:26:31.900 に答える
2

ImageViewを次のように宣言しました

ImageView view;

ImageView に Image を設定する必要があります。このようになるはずです

view = (ImageView) findViewById(R.id.single_image);
view.setImageBitmap(bmImg);
于 2012-08-28T14:17:14.433 に答える
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);

于 2015-10-07T04:46:58.350 に答える