0

i already create a code to save image from webview. the code works but there are some problem, here is the code

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        final WebView.HitTestResult result = mWebView.getHitTestResult();
        if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

            //menu.setHeaderTitle(result.getExtra());
            menu.add(0, 1, 0, "Save Image")
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {

                            String DownloadImageURL = result.getExtra();

                            if(URLUtil.isValidUrl(DownloadImageURL)){

                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                                downloadManager.enqueue(request);

                                Toast.makeText(getContext(),"Image save successfully.",Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(getContext(),"Error to save image.",Toast.LENGTH_SHORT).show();
                            }
                            return false;
                        }
                    });
        }
    }

when i try to long click the image it will show dialog message to save the image, when i click on it, it will show toast message that says the image have been save succesfully and yes there a notification in my stat bar

stat bar notif

the problem is, when i try to open my gallery and search for the image, i never find the image because it's not save to my local storage.

can you help me to fix the code? thanks

Poeja Network
  • 184
  • 2
  • 17

2 Answers2

0

Try calling this method from your onMenuItemClick after String DownloadImageURL = result.getExtra();

public void saveImage(){
try {
  URL url = new URL(yourImageUrl);
  InputStream is = (InputStream) url.getContent();
  byte[] buffer = new byte[8192];
  int bytesRead;
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  while ((bytesRead = is.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
  }
  //return output.toByteArray();
  Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (MalformedURLException e) {
e.printStackTrace();
//return null;
} catch (IOException e) {
e.printStackTrace();
//return null;
}
FileOutputStream out = null;
try {
    out = new FileOutputStream(filename); //some String filename
    bm.compress(Bitmap.CompressFormat.PNG, 100, out); // bm is your Bitmap instance
    // PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

However the above method is not tested at all. I have googled a little just because it looks like you needed some more help. Hope this solves your problem.

Simo
  • 345
  • 4
  • 12
0

Tested your code and i got many error, but i already solved my problem

The gallery never show the downloaded images because storage scanner never scan them, in fact they are there in our manager. So what i did is to send a broadcast to tell storage that new images is come to storage

Poeja Network
  • 184
  • 2
  • 17