In my application I am using a webview to use Facebook. In Facebook page we will find many posts with images. so to download the images from Facebook, I am using longpress method and HitTestResult method for the webview. My long press is working and HitTestResult is having the data where i have longpressed. I am getting Url's(text with https links) properly.
My problem is that if I longpressed on facebook image post(simply on images), i am not getting the images, instead I am getting the url links (like-https://something). how to get access to the images and download them.
Permissions I added in manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
In my activity:
WebSettings settings = ((WebView) findViewById(R.id.webview)).getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setLoadWithOverviewMode(true);
webView2.setWebViewClient(new PQClient());
webView2.setWebChromeClient(new PQChromeClient());
and
@Override
protected void onResume() {
    super.onResume();
    final WebView wv = (WebView) findViewById(R.id.webview);
    wv.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                WebView.HitTestResult hitResult = null;
                hitResult = wv.getHitTestResult();
                if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                        hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    try {
                        String imageUrl = hitResult.getExtra();
                        Toast.makeText(SocialMediaActivity.this,imageUrl,Toast.LENGTH_LONG).show();
                        URL url = new URL(imageUrl);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setRequestMethod("GET");
                        urlConnection.setDoOutput(true);
                        urlConnection.connect();
                        File sdcard = Environment.getExternalStorageDirectory();
                        File file = new File(sdcard, "filename.ext");
                        FileOutputStream fileOutput = new FileOutputStream(file);
                        InputStream inputStream = urlConnection.getInputStream();
                        byte[] buffer = new byte[1024];
                        int bufferLength = 0;
                        while ((bufferLength = inputStream.read(buffer)) > 0) {
                            fileOutput.write(buffer, 0, bufferLength);
                        }
                        fileOutput.close();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        });
    }
Here, I think the problem is in if condition because where ever i performed longpress, it is not returning the image but giving the url link. If i get the image i can download that to local SD card.
if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                    hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)
I tried the code with Bitmap also to get the image.
     @Override
protected void onResume() {
    super.onResume();
    final WebView wv = (WebView) findViewById(R.id.webview);
    wv.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                WebView.HitTestResult hitResult = null;
                hitResult = wv.getHitTestResult();
                    try {
                        String imageUrl = hitResult.getExtra();
                        URL url = new URL(imageUrl);
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        Toast.makeText(SocialMediaActivity.this,imageUrl,Toast.LENGTH_LONG).show();
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        image.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                    //you can create a new file name "test.jpg" in sdcard folder.
                        File f = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
                        f.createNewFile();
                    //write the bytes in file
                        FileOutputStream fo = new FileOutputStream(f);
                        fo.write(bytes.toByteArray());
                    // remember close de FileOutput
                        fo.close();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                return true;
            }
        });
    }
My application is crashing if i use this code.
 
     
    