如何动态地将字符串数组中的图像添加到活动的scrollview线性布局中

时间:2016-03-22 20:20:05

标签: android android-asynctask imageview scrollview

我有一个字符串资源,其中包含jpg格式的一堆图像的url,作为string.xml中的字符串数组资源。我试图动态地将这些图像添加到活动中。我首先尝试添加链接的文本视图,这是有效的。但是当我尝试实际添加图像时,它给了我一个android.os.NetworkOnMainThreadException。为了解决这个问题,我添加了一个异步任务,但这仍然没有用。请告诉我我做错了什么。我是Android编程的新手,所以如果这是一个愚蠢的问题,请原谅我。这是我到目前为止的代码:

package com.example.testgallery;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

public class Gallery extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        List<String> pictures = Arrays.asList(getResources().getStringArray(R.array.gallery_2014));
        LinearLayout piclayout = (LinearLayout) findViewById(R.id.gallery_preview_scroll_layout);
        for(int x=0;x<pictures.size();x++) {
            String url = pictures.get(x);
            TextView urlview = new TextView(this);
            urlview.setText(url);
            piclayout.addView(urlview);
            try {
                new getPictures().execute(url);
            } catch (Exception e) {
                Log.d("Picture Error",e.toString());
            }
        }
    }

    public void addImage(InputStream is){
        Drawable d = Drawable.createFromStream(is, "Picture");
        ImageView image = new ImageView(Gallery.this);
        image.setImageDrawable(d);
        LinearLayout piclayout = (LinearLayout) findViewById(R.id.gallery_preview_scroll_layout);
        piclayout.addView(image);
    }

    class getPictures extends AsyncTask<String, Void, InputStream> {

        private Exception exception;

        protected InputStream doInBackground(String... urls) {
            InputStream is = null;
            for(String url : urls) {
                try {
                    is = (InputStream) new URL(url).getContent();
                } catch (Exception e) {
                    Log.d("Picture Error", e.toString());
                }
            }
            return is;
        }

        protected void onPostExecute(InputStream is) {
            addImage(is);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我一直在努力,想出了一个快速解决方案。这可能不是最好的解决方案,但它有效:

package com.example.testgallety

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class Gallery extends AppCompatActivity {

    List<String> urls;
    Bitmap bmImg = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        urls = Arrays.asList(getResources().getStringArray(R.array.gallery_2014));
        for(int i=0;i<urls.size();i++){
            String url;
            url = urls.get(i);
            new getPictures().execute(url);
        }
    }

    public void addImage(InputStream is){

    }

    class getPictures extends AsyncTask<String, String, String> {

        private Exception exception;

        @Override
        protected void onPreExecute(){
            super.onPreExecute();
        }

        protected String doInBackground(String... args) {
            InputStream is = null;
            URL uri;
            String url = args[0];
            try {
                uri = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
                conn.setDoInput(true);
                conn.connect();
                is = conn.getInputStream();
                bmImg=BitmapFactory.decodeStream(is);
            } catch (Exception e) {
                Log.d("Picture Error", e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super .onPostExecute(result);
            ImageView image;
            LinearLayout piclayout = (LinearLayout) findViewById(R.id.gallery_preview_scroll_layout);
            image = new ImageView(Gallery.this);
            image.setImageBitmap(bmImg);
            piclayout.addView(image);
        }
    }

}