如何使用位图数组来设置的ImageView [英] How to set ImageView using Bitmap Array

查看:178
本文介绍了如何使用位图数组来设置的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的越来越图像的URL的一个演示通过 JSON 和得到后 ULR 我下载该图片并显示到 GridView控件。用于存储下载的图像我使用位图数组并将它传递给 ImageAdapter 。现在我想显示对的ImageView的下一个活动图像为我用下面的第二个活动code ..

I'm doing one demo of getting Image URLs through JSON and after getting the ULR I'm downloading that images and display them into the GridView. For storing downloaded images I'm using Bitmap array and passing it to ImageAdapter. And now I want to display that images on next activity on ImageView for that I'm using following code in second activity..

        // get intent data
    Intent i = getIntent();
    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setBackgroundDrawable(imageAdapter.bm[position]);

但它给我像错误类型的ImageView的方法setImageDrawable(绘制对象)不适用的参数(位图)。请告诉我,我怎么可以设置从位图阵列的ImageView ..

but It giving me error like The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (Bitmap). Please tell me how can I set the Image from Bitmap array to ImageView..

ImageAdapter类:

ImageAdapter Class:

    public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public Bitmap bm[]=new Bitmap[4];
    ImageView imageView;

    // Constructor
    public ImageAdapter(Context c, Bitmap[] bm){
        mContext = c;
        this.bm = bm; 
    }


    public ImageAdapter(Context c) {
        mContext = c;
    }


    @Override
    public int getCount() {
        return bm.length;
    }

    @Override
    public Object getItem(int position) {
        return bm[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        imageView = new ImageView(mContext);
        imageView.setImageBitmap(bm[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;
    }



}

主类:

    public class AndroidGridLayoutActivity extends Activity {

    public static final  Bitmap bm[]=new Bitmap[4];
    public static  ImageAdapter img;
    public static  String[] imageurls;
    public static  GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        Log.d("LOG", "1");
        new GetImageUrls().execute();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }       
        Log.d("LOG", "7");
        gridView = (GridView) findViewById(R.id.grid_view);
        // Instance of ImageAdapter Class
        Log.d("LOG", "8");
        img = new ImageAdapter(getApplicationContext(), bm);
        Log.d("LOG", "9");
        gridView.setAdapter(img);
        Log.d("LOG", "10");


        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Log.d("LOG", "11");
                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                Log.d("LOG", "12");
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }

    public class GetImageUrls  extends AsyncTask<Void, Void, Void>
    {
        Context context;
        private ProgressDialog pDialog;
        // URL to get JSON
        private static final String url= "http://xxx.x.xxx/demo/test.json";
        private static final String MAIN = "mainCategory";
        private static final String IMAGE = "mcatimage";
        // JSONArray
        JSONArray loginjsonarray=null;
        //result from url

        protected void onPreExecute() {
            Log.d("LOG", "2");
            // Showing progress dialog
            pDialog = new ProgressDialog(getApplicationContext());
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(false);
            //pDialog.show();
            Log.d("LOG", "3");
        }
        protected Void doInBackground(Void... arg) {
            Log.d("LOG", "4");
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();
            Log.d("LOG", "5");
             // Making a request to url and getting response
            String jsonstr = sh.makeServiceCall(url, ServiceHandler.POST, null);
            Log.d("Response: ", ">"+jsonstr);
            if(jsonstr!=null)
            {
                try {
                        JSONObject jsonObj =new JSONObject(jsonstr);
                        loginjsonarray=jsonObj.getJSONArray(MAIN);
                        imageurls=new String[loginjsonarray.length()];
                        for(int i=0;i<loginjsonarray.length();i++){
                            JSONObject l=loginjsonarray.getJSONObject(i);
                            imageurls[i]=l.getString(IMAGE);
                            Log.d("imageurls: ", imageurls[i]);
                        }
                        for(int i=0;i<imageurls.length;i++)
                        {   
                            bm[i]=DownloadImage(imageurls[i]);
                            Log.d("LOG", bm[i].toString());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }else{
                Toast.makeText(context,"Check your Internet Connection",Toast.LENGTH_SHORT).show();
            }
            return null;
        }


        public Bitmap DownloadImage(String STRURL) {
            Bitmap bitmap = null;
            InputStream in = null;       
            try {
                    int response = -1;
                    URL url = new URL(STRURL);
                    Log.d("DownloadImage: ", url.toString());
                    URLConnection conn = url.openConnection();
                    if (!(conn instanceof HttpURLConnection))             
                        throw new IOException("Not an HTTP connection");
                    try{
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.connect();
                        response = httpConn.getResponseCode();  
                        Log.d("DownloadImage response: ", Integer.toString(response));
                        if (response == HttpURLConnection.HTTP_OK) {
                            in = httpConn.getInputStream();
                            Log.d("DownloadImage: ", in.toString());
                        }                    
                    } catch (Exception ex) {
                        throw new IOException("Error connecting"); 
                    }
                    bitmap = BitmapFactory.decodeStream(in);
                    Log.d("DownloadImage Bitmap: ", bitmap.toString());
                    in.close();
                }catch (IOException e1) {
                    e1.printStackTrace();
                }
            Log.d("LOG", "6");
            return bitmap; 
        }

        protected void onPostExecute(Integer result) {
            // Dismiss the progress dialog
            if(pDialog.isShowing()){
                pDialog.dismiss();
            }

        }
    }

}

FullActivity类:

FullActivity Class:

public class FullImageActivity extends Activity {


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

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    Log.d("Position", Integer.toString(position));
    ImageAdapter imageAdapter = new ImageAdapter(this);
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageBitmap(imageAdapter.bm[position]);
}

}

推荐答案

它如此简单的方式使用<一个href=\"http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap%28android.graphics.Bitmap%29\"相对=nofollow> setImageBitmap ..

Its so simple way use setImageBitmap..

imageView.setImageBitmap(imageAdapter.bm[position]);

这篇关于如何使用位图数组来设置的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆