Android Open Source - json_resume Icon Download Task






From Project

Back to project page json_resume.

License

The source code is released under:

/* * Copyright (c) 2015 Hunter Davis <hunter@hunterdavis.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided th...

If you think the Android project json_resume listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hunterdavis.jsonresumeviewer;
/*from  ww w . ja  v a  2 s .com*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * We have to do this instead of using picasso.. why?
 * -- because picasso won't render favico.ico files. ugh
 */
public class IconDownloadTask extends AsyncTask<String, Void, Bitmap> {
    private final WeakReference imageViewReference;


    public IconDownloadTask(ImageView imageView) {
        imageViewReference = new WeakReference(imageView);
    }

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
        // params comes from the execute() call: params[0] is the url.
        return getBitmapFromURL(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = (ImageView) imageViewReference.get();
            if (imageView != null) {

                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    imageView.setVisibility(View.VISIBLE);
                }
            }

        }
    }

    public Bitmap getBitmapFromURL(String address) {
        try {
            URL url = new URL(address);

            Request request = new Request.Builder()
                    .url(url)
                    .build();

            Response response = JsonResumeActivity.client.newCall(request).execute();
            InputStream input = response.body().byteStream();

            BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap myBitmap = BitmapFactory.decodeStream(input, null, options);

            return myBitmap;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}




Java Source Code List

com.hunterdavis.jsonresumeviewer.IconDownloadTask.java
com.hunterdavis.jsonresumeviewer.JsonResumeActivity.java
com.hunterdavis.jsonresumeviewer.JsonResumeParser.java
com.hunterdavis.jsonresumeviewer.ResumePagerAdapter.java
com.hunterdavis.jsonresumeviewer.ResumeSections.java
com.hunterdavis.jsonresumeviewer.fragment.AwardsFragment.java
com.hunterdavis.jsonresumeviewer.fragment.BasicsResumeFragment.java
com.hunterdavis.jsonresumeviewer.fragment.EducationFragment.java
com.hunterdavis.jsonresumeviewer.fragment.InterestsFragment.java
com.hunterdavis.jsonresumeviewer.fragment.LanguagesFragment.java
com.hunterdavis.jsonresumeviewer.fragment.ProfileFragment.java
com.hunterdavis.jsonresumeviewer.fragment.PublicationsFragment.java
com.hunterdavis.jsonresumeviewer.fragment.ReferencesFragment.java
com.hunterdavis.jsonresumeviewer.fragment.SkillsFragment.java
com.hunterdavis.jsonresumeviewer.fragment.TextResumeFragment.java
com.hunterdavis.jsonresumeviewer.fragment.VolunteerFragment.java
com.hunterdavis.jsonresumeviewer.fragment.WorkFragment.java
com.hunterdavis.jsonresumeviewer.types.Awards.java
com.hunterdavis.jsonresumeviewer.types.Basics.java
com.hunterdavis.jsonresumeviewer.types.Education.java
com.hunterdavis.jsonresumeviewer.types.Interests.java
com.hunterdavis.jsonresumeviewer.types.Languages.java
com.hunterdavis.jsonresumeviewer.types.Location.java
com.hunterdavis.jsonresumeviewer.types.Profiles.java
com.hunterdavis.jsonresumeviewer.types.Publications.java
com.hunterdavis.jsonresumeviewer.types.References.java
com.hunterdavis.jsonresumeviewer.types.Resume.java
com.hunterdavis.jsonresumeviewer.types.Skills.java
com.hunterdavis.jsonresumeviewer.types.Volunteer.java
com.hunterdavis.jsonresumeviewer.types.Work.java