Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:37:04+00:00 2026-06-14T14:37:04+00:00

I’m new to Android and Java. I have been working on my task i.e,

  • 0

I’m new to Android and Java. I have been working on my task i.e, Image Downloader. Where I have to download images with progress bar and display them in grid View. I have created two classes 1. URLImageAdapter 2. CacheActivity. Everything works fine but now I want to save those images to sd card. I have been looking on what methods and changes should be made? Any help? Thank You. I have added permission in Android Manifest File.

public class URLImageAdapter extends BaseAdapter {

private class Image {
    String url;
    Bitmap thumb;
}

private Image[] images;
private Context myContext;
public LoadThumbsTask thumbnailGen;
private Object previousList;


public URLImageAdapter(Context c) {
    myContext = c;
    thumbnailGen = new LoadThumbsTask();

    if (previousList != null) {
        images = (Image[]) previousList;
        thumbnailGen.execute(images);
        return;
    }

    images = new Image[imageURLs.length];

    for (int i = 0, j = imageURLs.length; i < j; i++) {
        images[i] = new Image();
        images[i].url = imageURLs[i];
    }

    thumbnailGen.execute(images);

}

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

public Object getItem(int position) {
    return images[position].url;
}

public long getItemId(int position) {
    return position;
}

public Object getData() {
    if (thumbnailGen != null
            && thumbnailGen.getStatus() != AsyncTask.Status.FINISHED) {
        thumbnailGen.cancel(true);
    }

    return images;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imgView;

    Image cached = images[position];

    if (convertView == null) {

        imgView = new ImageView(myContext);
        imgView.setLayoutParams(new GridView.LayoutParams(100, 100));

    } else {

        imgView = (ImageView) convertView;

    }

    if (cached.thumb == null) {

        imgView.setImageResource(R.drawable.ic_action_search);
        imgView.setScaleType(ScaleType.CENTER);

    } else {

        imgView.setScaleType(ScaleType.FIT_CENTER);
        imgView.setImageBitmap(cached.thumb);

    }

    return imgView;
}

private void cacheUpdated() {
    this.notifyDataSetChanged();
}


private Bitmap loadThumb(String url) {

    Bitmap thumb = null;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 4;

    try {

        URL u = new URL(url);
        URLConnection c = u.openConnection();
        c.connect();

        BufferedInputStream stream = new BufferedInputStream(
                c.getInputStream());

        thumb = BitmapFactory.decodeStream(stream, null, opts);


        stream.close();

    } catch (MalformedURLException e) {
        Log.e("ERROR", "malformed url: " + url);
    } catch (IOException e) {
        Log.e("ERROR", "An error has occurred downloading the image: "
                + url);
    }

    return thumb;
}

private class LoadThumbsTask extends AsyncTask<Image, Void, Void> {

    /*private ProgressDialog dialog;
      @Override
        protected void onPreExecute() {
        this.dialog = ProgressDialog.show(myContext, "Please wait",
            "Downloading.....", true);
                }

            @Override
            protected void onPostExecute(Void unused) {
    //Intent for next activity
     this.dialog.dismiss();
    }*/

    @Override
    protected Void doInBackground(Image... cache) {

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 4;

        for (Image i : cache) {

            if (isCancelled())
                return null;

            if (i.thumb != null)
                continue;

            SystemClock.sleep(500);

            i.thumb = loadThumb(i.url);

            publishProgress();
        }

        return null;

    }

    @Override
    protected void onProgressUpdate(Void... param) {
        cacheUpdated();
    }
}

private String[] imageURLs = {
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2851.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2944.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2989.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3005.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3012.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3034.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3047.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3092.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3110.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3113.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3128.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3160.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3226.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3228.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3251.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3268.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3275.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3346.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3365.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3374.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3385.jpg",
        "http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3392.jpg", };
 }

CacheActivity

public class CacheActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private GridView gridview;
private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cache);
     gridview = (GridView) findViewById(R.id.grid_view);
     addListenerOnButton();
}




public void addListenerOnButton() {

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
    btnStartProgress.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("File downloading ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            progressBarStatus = 0;

            fileSize = 0;

            new Thread(new Runnable() {
                public void run() {
                    while (progressBarStatus < 100) {

                        progressBarStatus = doInBackground();


                        try {
                            Thread.sleep(7500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    if (progressBarStatus >= 100) {

                        try {
                            Thread.sleep(450);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        progressBar.dismiss();

                    }
                }
            }).start();
            try {
                gridview.setAdapter(new URLImageAdapter(CacheActivity.this));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });

}

public int doInBackground() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;
        } else if (fileSize == 200000) {
            return 20;
        } else if (fileSize == 300000) {
            return 30;
        }else if (fileSize == 400000) {
            return 40;
        }else if (fileSize == 500000) {
            return 50;
        }else if (fileSize == 600000) {
            return 60;
        }else if (fileSize == 700000) {
            return 70;
        }else if (fileSize == 800000) {
            return 80;
        }else if (fileSize == 900000) {
            return 90;
        }

    }

    return 100;

}

 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:37:05+00:00Added an answer on June 14, 2026 at 2:37 pm

    call this below methode in getView() under

            imgView.setScaleType(ScaleType.FIT_CENTER);
            imgView.setImageBitmap(cached.thumb);
            saveDataInSdCard(cached.thumb,position);
    
    
    private void saveDataInSdCard(Bitmap bt,int i) {
        OutputStream fOut = null;
        Uri outputFileUri;
        try {
            File root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "urFlodername" + File.separator);
            root.mkdirs();
            sdImageMainDirectory = new File(root,i+"myPicName.jpg");
            outputFileUri = Uri.fromFile(sdImageMainDirectory);
            fOut = new FileOutputStream(sdImageMainDirectory);
        } catch (Exception e) {
    
        }
    
        Bitmap bm =bt;
        try {
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
        }       
    
    }
    
    
    
     add permission    
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.