简体   繁体   中英

Save image to sdcard in Android

I have a ImageView and a WebView where I am trying to show the same image.

I have downloaded the image with picasso and all works well during the download.

When I save the image into sdcard to show into a ImageView and into WebView I have a problem: My ImageView allways shows the last image downloaded and this is ok. But, after I save the image to show into WebView, my WebView always shows the first image downloaded and not the last downloaded.

Please, what am I doing wrong?

    public void downloadimage(){
      Bitmap bm=MyGetImage(1);
      saveImageToExternalStorageToShow(bm);

      bm=MyGetImage(2);
      saveImageToExternalStorageToShow(bm);//into this method, webview doesn´t show image correctly
    }

    public void saveImageToExternalStorageToShow(Bitmap image) {
        String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
        try
        {
            ImageView imv= (ImageView) getActivity().findViewById(R.id.ImvTeste);
           //ok-shows the image downloaded 
           imv.setImageBitmap(image);

            File dir = new File(fullPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            OutputStream fOut = null;
            String fileName = "sala.jpg"; //DateFormat.format("dd_MM_yyyy_hh_mm_ss", System.currentTimeMillis()).toString();
            File file = new File(fullPath, fileName);

            boolean b=false;
            if(!file.exists()) {
                b=file.createNewFile();
            } else {
                 b=file.delete();
                 b=file.createNewFile();
            }

            fOut = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
            fOut.flush();
            fOut.close();

            String imagePath = Uri.fromFile(file).toString();//"file://"+ base + "/test.jpg";
            String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
            //Problem here-wedview allways shows the first image downloaded
            webView.loadDataWithBaseURL("", html, "text/html", "utf-8", ""); 
        }
        catch (Exception e)
        {
            Log.e("saveToExternalStorage()", e.getMessage());
        }
    }

This is likely due to the image being cached in the WebView.

Try calling webView.clearCache(true) before showing the second image and it should work propperly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM