保存并共享图像universal-image-loader

时间:2013-01-30 15:36:27

标签: android

我正在使用通用图像加载器来显示来自网址的多个图像,在ImagePager Activity中我想添加“保存”和“共享”按钮,但是在哪里尝试编写代码我有多个错误?任何人都可以知道我可以在哪里使用这些功能以及它们的最佳代码是什么?

ImagePagerActivity.java

package com.nostra13.example.universalimageloader;
import static com.nostra13.example.universalimageloader.Constants.IMAGES;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;


public class ImagePagerActivity extends BaseActivity {

DisplayImageOptions options;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_pager);

    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

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

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



}




private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

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

    @Override
    public void destroyItem(View container, int position, Object object) {
        View rootView = (View) object;
        ImageView imageView = (ImageView) rootView.findViewById(R.id.image);
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();

        if (drawable != null) {
            Bitmap bitmap = drawable.getBitmap();
            if (bitmap != null) {
                bitmap.recycle();
            }

        }

        ((ViewPager) container).removeView(rootView);
    }

    @Override
    public void finishUpdate(View container) {
    }

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

    @Override
    public Object instantiateItem(View view, int position) {
        final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
        final 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() {
                spinner.setVisibility(View.VISIBLE);


            }

            @Override
            public void onLoadingFailed(FailReason failReason) {
                String message = null;
                switch (failReason) {
                    case IO_ERROR:
                        message = "Input/Output error";
                        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);
                imageView.setImageResource(R.drawable.ic_error);
            }

            @Override
            public void onLoadingComplete(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) {
    }

}
}

1 个答案:

答案 0 :(得分:2)

Bitmap mSaveBit = imageLoader.getMemoryCache().get(Constants.IMAGESURLS[pager.getCurrentItem()]);
                    File imageFile = null;
                    if(null==mSaveBit){
                        imageFile =  imageLoader.getDiscCache().get(Constants.IMAGESURLS[pager.getCurrentItem()]);
                     }

你可以试试这个。我成功获得了。