Cancel Async Task while downloading image
<p><strong>Hi, 
I have an async task which downloads image via http request and shares it after completion. But if the user cancel the task, it should stop.</strong></p>

<p><strong>I'm calling it like this:</strong></p>

<pre><code>mShareImage = new shareAsync(PhotoEnlargeActivity.this).execute(imageUris.get(currentPosition));
</code></pre>

<p><strong>And stopping it like this:</strong></p>

<pre><code>mShareImage.cancel(true);
</code></pre>

<p><strong>But it doesn't seen to work. Async Task:</strong></p>

<pre><code>public class shareAsync extends AsyncTask&lt;String, String, String&gt; {
    private Context mContext;
    URL myFileUrl;

    Bitmap bmImg = null;
    Intent share;
    File file;
    boolean isCancelled = false;

    public shareAsync(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        isCancelled = true;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();
        showProgressDialog("Downloading High Resolution Image for Sharing...");

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        HttpURLConnection conn = null;

        try {
            if (!isCancelled()) {
                myFileUrl = new URL(args[0]);
                conn = (HttpURLConnection) myFileUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                bmImg = BitmapFactory.decodeStream(is);
            } else {
                if (conn != null) conn.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath()
                    + "/Google Image Wallpaper/");
            dir.mkdirs();
            String fileName = idStr;
            file = new File(dir, fileName);
            FileOutputStream fos = new FileOutputStream(file);
            bmImg.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();

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

        return null;
    }

    @Override
    protected void onPostExecute(String args) {
        // TODO Auto-generated method stub
        progressDialog.dismiss();
        share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");

        share.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath().toString()));

        mContext.startActivity(Intent.createChooser(share, "Share Image"));

    }

}
</code></pre>