2

我正在尝试使用通用图像加载器从 flickr 中的照片集中获取照片列表我的基本活动 ImagePagerActivity 调用 FetchPhotos,它扩展了异步任务。代码如下

    public class ImagePagerActivity extends BaseActivity {

private static final String STATE_POSITION = "STATE_POSITION";
public static final String API_KEY="mykey";
public static final String USER_ID="myid";
DisplayImageOptions options;
private Photos thePhotoList;
ViewPager pager;

private String thePhotos="";
private final int[] timeout={3,10};
private String url="http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&format=json&api_key="+API_KEY+"&photoset_id=111111111";
private  String[] imageUrls;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_pager);

    Bundle bundle = getIntent().getExtras();
    String url="http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&format=json&api_key="+API_KEY+"&photoset_id=72157633359614452";
    setContentView(R.layout.pics);

    try{
        ((ViewAnimator)findViewById(R.id.PictureAnimator)).removeAllViews();
    }catch (Exception e) {}
    thePhotoList = new Photos(url);
    thePhotoList.execute();

    imageUrls=thePhotoList.imageList;
    //imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    if (savedInstanceState != null) {
        pagerPosition = savedInstanceState.getInt(STATE_POSITION);
    }

    options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.ic_empty)
        .showImageOnFail(R.drawable.ic_error)
        .resetViewBeforeLoading()
        .cacheOnDisc()
        .imageScaleType(ImageScaleType.EXACTLY)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .displayer(new FadeInBitmapDisplayer(300))
        .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(this.imageUrls));
    pager.setCurrentItem(pagerPosition);
}

private class Photos extends com.flickr.FetchPhotos{
    @Override
    public void onFetchError() {}
    public Photos(String url) {super(ImagePagerActivity.this, url);}    
}
@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt(STATE_POSITION, pager.getCurrentItem());
}

private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

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

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
        ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

        imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                spinner.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason.getType()) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        break;
                    case DECODING_ERROR:
                        message = "Image can't be decoded";
                        break;
                    case NETWORK_DENIED:
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY:
                        message = "Out Of Memory error";
                        break;
                    case UNKNOWN:
                        message = "Unknown error";
                        break;
                }
                Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                spinner.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
            }
        });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

}

接下来是 FetchPhotos 类

     public abstract class FetchPhotos extends AsyncTask<Void, Void, Boolean>{

private Context context;
private ProgressDialog pd;
private String thePhotos="";
private String url;
private final int[] timeout={3,10};

public ArrayList<PictureInfo> thePics;
public String[] imageList;

public FetchPhotos(Context context,String url) {
    this.context=context;   
    this.url=url;

}


public String[] fillGalery(JSONObject theFeed) {
    // TODO Auto-generated method stub
    String[] imageUrls = null;
    try{
    JSONArray  Categories=theFeed.getJSONArray("photo");
    imageUrls=new String[Categories.length()];
    for (int i=0;i<(Categories.length()>15?15:Categories.length());i++){
        JSONObject pic = Categories.getJSONObject(i);
        String url1="http://farm"+pic.getString("farm")+".staticflickr.com/"+pic.getString("server")+"/"+
                pic.getString("id")+"_"+pic.getString("secret")+".jpg";
        imageUrls[i]=url1;
        System.out.println(imageUrls[i]);

    }

    return imageUrls;
    }
    catch(Exception e){

    }
    return imageUrls;

}

@Override
protected void onPreExecute() {
    pd=ProgressDialog.show(context, "downloading", "please wait");
    super.onPreExecute();
}

@Override
protected Boolean doInBackground(Void... arg0) {
    try{
        thePhotos = new Internet().GetRequest(url, null, timeout);
        return true;
    }catch (Exception e) {
        return false;
    }
}

@Override
protected void onPostExecute(Boolean result) {
    pd.dismiss();
    if(result){
        try {
            thePhotos=thePhotos.split("\\(")[1];
            thePhotos.replace("\\)", "");
            imageList=fillGalery(new JSONObject(thePhotos).getJSONObject("photoset"));
        } catch (Exception e) {Log.e("karp", "photolist2: "+e.getMessage());onFetchError();onFetchError();}
    }else{
        onFetchError();
    }
    super.onPostExecute(result);
}


public abstract void onFetchError();

public void LoadPhoto(PictureInfo pi){
    Log.d("karp", "LoadPhoto");
    if(!(pi.executed)){
        new LoadPics(pi).execute();
        pi.executed=true;
    }
}


private class LoadPics extends RemoteImage{
    private ImageView ivTarget;
    private ProgressBar pb;
    public LoadPics(PictureInfo pi) {
        super(pi.url);
        this.ivTarget=pi.iv;
        this.pb=pi.pb;
    }       
    @Override
    public void onSuccess(Bitmap remoteBitmap) {
        try{
            pb.setVisibility(View.INVISIBLE);
            ivTarget.setImageBitmap(remoteBitmap);  
        }catch (Exception e) {}
    }       
    @Override
    public void onFail() {pb.setVisibility(View.INVISIBLE);}
}

}

我已经在 Image Pager 中创建了 Photos 类,然后我尝试访问填充库现在我尝试使用 reurns 字符串数组的方法 fillGallery 用来自 flickr 的图像 url 填充字符串数组

在我的基本活动中,我打电话

thePhotoList = new Photos(url);
    thePhotoList.execute();

    imageUrls=thePhotoList.imageList;

但尽我所能,我无法在 imageUrls 中得到一个数组,它是一个字符串数组。

当我使用硬编码字符串数组和其中的图像 url 时,代码有效。任何帮助将非常感激。我确定我在做一些非常愚蠢的事情,因为我是新手。非常感谢 。干杯!

4

1 回答 1

0

我让它工作了。需要添加监听接口,从onPostExecute调用方法,在activity中实现,在监听抽象方法的实现中访问变量。在发布之前应该进行更多研究,但问题被困太久了。道歉。

于 2013-04-28T19:15:20.780 回答