-1

I want to download image in HTML with my class in webview. how to do? my web load in baseurl. my class download image from internet. please show with example thanks

2 Answers2

0

Use this:webviewId.loadDataWithBaseURL("", response, "text/html", "UTF-8", "");

OR

Html.ImageGetter() use this, this will search <img> tag 

http://developer.android.com/reference/android/text/Html.ImageGetter.html

Example links:

Html.ImageGetter

Android HTML ImageGetter as AsyncTask

Community
  • 1
  • 1
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • I want download image in HTML with my class?your answer not for it. – user2790938 Jan 17 '14 at 11:02
  • what do you mean "download image in HTML with my class"? Could you explain it? – rupesh Jan 17 '14 at 11:09
  • if image exist in HTML then download image by won class? – user2790938 Jan 17 '14 at 11:13
  • If i am not wrong you want to download images with HTML tag and want to display in Webview that means you want to show in offline mode too. So you can not do that but you can simply store full HTML response in database and show in Webview, so for some times images will be visible that means get cached but after some time it won't be visible. So once users will again get online same thing will happen. – rupesh Jan 17 '14 at 11:16
  • I want to download handler image? – user2790938 Jan 17 '14 at 11:18
0
    public class ImageGetter  implements Html.ImageGetter {
        Context c;
        View container;

        public ImageGetter(View t, Context c) {
            this.c = c;
            this.container = t;
        }

        public Drawable getDrawable(String source) {
            URLDrawable urlDrawable = new URLDrawable();

            // get the actual source
            ImageGetterAsyncTask asyncTask =
                new ImageGetterAsyncTask( urlDrawable);

        asyncTask.execute(source);

        // return reference to URLDrawable where I will change with actual image from
        // the src tag
        return urlDrawable;
    }

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
        URLDrawable urlDrawable;

        public ImageGetterAsyncTask(URLDrawable d) {
            this.urlDrawable = d;
        }

        @Override
        protected Drawable doInBackground(String... params) {
            String source = params[0];
            return fetchDrawable(source);
        }

        @Override
        protected void onPostExecute(Drawable result) {
            // set the correct bound according to the result from HTTP call

            urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0
                    + result.getIntrinsicHeight());


            // change the reference of the current drawable to the result
            // from the HTTP call
            urlDrawable.drawable = result;

            // redraw the image by invalidating the container
            ImageGetter.this.container.invalidate();
        }


        public Drawable fetchDrawable(String urlString) {
            try {
                //Get image from online and save it to the storage.
                InputStream is = fetch(urlString);
                Drawable drawable = Drawable.createFromStream(is, "src");
                drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                        + drawable.getIntrinsicHeight());

                Bitmap bm = BitmapFactory.decodeStream( is);
                String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                File file = new File(extStorageDirectory, urlString+".PNG");
                FileOutputStream outStream = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();
                return drawable;
            } catch (Exception e) {
                // offline use the stored image
                String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                Bitmap bitmap = BitmapFactory.decodeFile(extStorageDirectory+urlString+".PNG");
                Drawable drawable = new BitmapDrawable(bitmap);
                drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
                        + drawable.getIntrinsicHeight());

                return drawable;
            }
        }

        private InputStream fetch(String urlString) throws IOException {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(urlString);
            HttpResponse response = httpClient.execute(request);
            return response.getEntity().getContent();
        }
    }
}
cz game
  • 81
  • 1
  • 5