在对话框中下载图像和显示

时间:2014-02-19 21:01:34

标签: android dialog onclicklistener universal-image-loader buttonclick

我正在尝试通过“Lazy Image Loader”将图像加载到图像视图中。我已经包含了两个代码片段来显示我正在调用它的方式。


---- ----- EDIT

现在使用通用图像加载程序1.8.6

Log.i(Utils.TAG, "Show Photo");
ImageLoader imgLoader = new ImageLoader(a);
imgLoader.DisplayImage(strUrl, image);

这就是我调用它的方式,因为@TalhaQ方式不起作用。说它不接受那些数量的参数


---- ORIGINAL --- 当我转到该URL时,Log cat显示有效的URL和图像。有什么想法吗?

Utils.java

public Dialog openFeed(final Activity a, final long id) {

    final Dialog dialog = new Dialog(a);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.feed_item);


    //Set the Title & Descr to the Item Selected
    TextView title = (TextView) dialog.findViewById(R.id.txtItemTitle);
    title.setText(strTitle);

    TextView descr = (TextView) dialog.findViewById(R.id.txtItemDescription);
    descr.setText(strDescr);

    ImageView image = (ImageView) dialog.findViewById(R.id.imgFeedImage);


    Log.i(TAG, "strUrl: " + a.getResources().getString(R.string.photoURL) + strUrl);

    ImageLoader imageLoader = new ImageLoader(a);
    imageLoader.DisplayImage(a.getResources().getString(R.string.photoURL) + strUrl, image);

    Button CommentButton = (Button) dialog.findViewById(R.id.dialogButtonComment);

    // if button is clicked, close the custom dialog
    CommentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();

            final AlertDialog addComment = Utils.openComments(a, id);
            addComment.show();
        }
    });

XML:

<ImageView
    android:id="@+id/imgFeedImage"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_alignLeft="@+id/txtItemDescription"
    android:layout_alignRight="@+id/txtItemDescription"
    android:layout_below="@+id/txtItemDescription"
    android:contentDescription="@string/itemicon"
    android:src="@drawable/gplus"
    android:scaleType="fitCenter"
    android:maxHeight="200dp"
    android:visibility="invisible" />

2 个答案:

答案 0 :(得分:1)

我也使用相同的Lazy Image Loader。我认为你没有正确传递上下文。

   int loader = R.drawable.loader;//Old image in the imageview
   ImageLoader imgLoader = new ImageLoader(ctx); // ctx is the context
   imgLoader.DisplayImage(image_url, loader, image);//image is the imageview,image_url is the url to image

查看我的输出。如果您仍需要任何帮助我附上我的样本。下载它。查看link

enter image description here

答案 1 :(得分:0)

当您在对话框中加载图像时,下载的图像中出现以下场景,因此您应该在图像加载器内部处理ClassCastException以下代码将帮助您。如果imageview位于列表中的内部,或者活动中的活动或imageview中加载

,则不会发生该异常
Activity a= (Activity)(photoToLoad.imageView.getContext());

您不能假设Context可以转换为Activity。因此异常:您正在尝试将一个实际上是ContextThemeWrapper的Context转换为Activity。

您可以替换

Activity a= (Activity)(photoToLoad.imageView.getContext());
a.runOnUiThread(bd);

与例如

photoToLoad.imageView.post(bd);

将Runnable发布到UI线程消息队列,类似于Activity runOnUiThread()

相关问题