从URL问题加载图像

时间:2011-08-01 05:56:29

标签: java android image

我有一个onClickListener应该调用一个显示静态网址和图像的类。我在被叫的课堂上举杯祝酒,但没有图片。 list.onClickListener应该调用应该发布图像的ShowImage类。请帮助我不确定我做错了什么。

注意*在我的main_layout.xml中我有一个imageview。

package com.flash_tattoo;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends Activity {

    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

         Bundle bundle = getIntent().getExtras();
         String jsonData = bundle.getString("jsonData");

                JSONArray jsonArray;
                try {
                    jsonArray = new JSONArray(jsonData);

                    String[] mStrings = new String[jsonArray.length()];
                    String[] mImages = new String[jsonArray.length()];
                    String[] mPath = new String[jsonArray.length()];

                    for (int i=0; i<jsonArray.length(); i++)
                    {
                        String url = jsonArray.getJSONArray(i).getString(2);
                        mStrings[i] = "http://www.2020mediaonline.com/tattoo/thumbnail_image/" + url;

                        String image_name = jsonArray.getJSONArray(i).getString(1);
                        mImages[i] = image_name;

                        String path_name = jsonArray.getJSONArray(i).getString(1);
                        mPath[i] = path_name;

                    }


                    list=(ListView)findViewById(R.id.list);
                    adapter=new LazyAdapter(this, mStrings, mImages);
                    list.setAdapter(adapter);


                    list.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){

                            ShowImage go = new ShowImage();

                            Toast.makeText(MainActivity.this, "In onClick", Toast.LENGTH_LONG).show();

                        }
                    });



                    Button b=(Button)findViewById(R.id.button1);
                    b.setOnClickListener(listener);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


    }

   class ShowImage extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        ImageView imgView =(ImageView)findViewById(R.id.ImageView01);

        Drawable drawable = LoadImageFromWebOperations("http://www.ansi.okstate.edu/breeds/other/llama/llama1.jpg");
        imgView.setImageDrawable(drawable);
        Toast.makeText(MainActivity.this, "show big image", Toast.LENGTH_LONG).show();
        }

        private Drawable LoadImageFromWebOperations(String url)
        {
            try
            {
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "src name");
                return d;
                }catch (Exception e) {
                System.out.println("Exc="+e);
                return null;
                }
            }
        }

    @Override
    public void onDestroy()
    {
        adapter.imageLoader.stopThread();
        list.setAdapter(null);
        super.onDestroy();
    }

    public OnClickListener listener=new OnClickListener(){
        @Override
        public void onClick(View arg0) {
        Intent myIntent = new Intent(MainActivity.this,flash_tattoo.class);
           startActivityForResult(myIntent, 0);
        }
    };

}

3 个答案:

答案 0 :(得分:3)

我建议您使用AsyncTask加载您的位图并将其填充到您的imageView,您可以通过以下链接找到更多详细信息:

http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

希望这可以帮到你。

答案 1 :(得分:2)

您无法使用构造函数来启动新活动。您必须使用startActivity()。所以我不明白为什么你期望ShowImage go = new ShowImage()做任何事情。它只是一个空的默认构造函数。

答案 2 :(得分:0)

This正是您要找的。感谢。