0

use android image loader for your application. got all set up except for saving pictures on a flash drive. I know how to save a bitmap but how to get it do not know. please tell me how do I save a picture?

public class ImagePagerActivity extends BaseActivity implements View.OnClickListener {

    private static final String STATE_POSITION = "STATE_POSITION";

    DisplayImageOptions options;
    Button btn;
    ViewPager pager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_pager);
        btn = (Button) findViewById(R.id.myBtn);
        btn.setOnClickListener(this);
        Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        String[] imageUrls = bundle.getStringArray(Constants.Extra.IMAGES);
        int pagerPosition = bundle.getInt(Constants.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(true)
            .cacheOnDisc(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .considerExifParams(true)
            .displayer(new FadeInBitmapDisplayer(300))
            .build();

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, pager.getCurrentItem());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.myBtn:
                //then I need to save the current image
                break;
        }
    }

    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) {
            container.removeView((View) object);
        }

        @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);
            assert imageLayout != null;
            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);
                }
            });

            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;
        }
    }
}
IP696
  • 181
  • 1
  • 2
  • 11
  • Do you want to download and save image on sdcard? –  Apr 07 '14 at 04:42
  • Take screenshot of your `Viewpager ` and save it to as a `Bitmap `. – Piyush Apr 07 '14 at 04:46
  • yes. picture is downloaded and displayed in the ViewPager. I want to just keep it on the SD card. No version of the screenshot is not suitable) – IP696 Apr 07 '14 at 04:48
  • I found a similar theme but there is no solution namely, in this line   Bitmap mSaveBit = imageLoader.getMemoryCache (); http://stackoverflow.com/questions/22716418/save-image-in-current-view-to-sdcard-using-android-universal-image-loader – IP696 Apr 07 '14 at 04:50
  • Have you tried with `DrawingCache`??? – Piyush Apr 07 '14 at 04:50

3 Answers3

2

To save image on SD Card:

void saveImage(Bitmap myBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

Source

You can get bitmap in below method of Universal Image Loader:

 @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    saveImage(loadedImage);
                }
Community
  • 1
  • 1
  • thanks of course but I know how to save pictures from the Bitmap. my question maybe you misunderstood. I can not get Bitmap from android image loader – IP696 Apr 07 '14 at 04:56
  • @IP696 I have updated my answer. In Universal Image loaded, there is method called onLoadingComplete() where you get bitmap of loaded image and you can save this bitmap to your sdcard. –  Apr 07 '14 at 05:04
  • thank you so much. understand the theory of how to do it. but do not quite understand exactly where I call this piece of code. – IP696 Apr 07 '14 at 05:10
  • I found this code Bitmap mSaveBit = imageLoader.getMemoryCache (). Get (Constants.IMAGES [pager.getCurrentItem ()]); but the picture is stored empty. – IP696 Apr 07 '14 at 05:14
  • cool! it works. but how do I make that picture persisted if you click on "save" button otherwise preserved? I do not need more – IP696 Apr 07 '14 at 05:31
1

I think you used universal image loader,you can directly save images in universal image loader cache.

  • yes))) I have a question that I'm using his name. please tell me how I can save the image of him??? – IP696 Apr 07 '14 at 04:59
0
/**
 * @author Pavan
 */
public class UILApplication extends Application {

    /** */
    @Override
    public void onCreate() {
        super.onCreate();

        /**
         * This configuration tuning is custom. You can tune every option, you
         * may tune some of them, or you can create default configuration by
         * ImageLoaderConfiguration.createDefault(this); method.
         */

        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .build();

        File dir = null;
        String directoryString = "demo";
        /*
         * File dir = new File(Environment.getExternalStorageDirectory() +
         * directoryString);
         */
        dir = getDir(directoryString, Activity.MODE_PRIVATE);
        if (dir != null && dir.exists() && dir.isDirectory()) {
            // directory exists

        }
        /** create new directory */
        else {
            boolean result = false;
            if (dir != null)
                result = dir.mkdir();
            else {

            }
        }

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .threadPoolSize(3)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCacheExtraOptions(150, 150)
                // default = device screen dimensions
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .discCacheExtraOptions(IMAGE_DIMENSIONS.MAX_IMAGE_SIZE,
                        IMAGE_DIMENSIONS.MAX_IMAGE_SIZE, CompressFormat.JPEG,
                        75)
                /** Adding a directory path to cache the images */
                .discCache(new UnlimitedDiscCache(dir))
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .enableLogging() // Not necessary in common
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }
}