在Android中将图像保存到SD卡

时间:2015-09-01 01:12:56

标签: android image webview save

我有一个ImageView和一个WebView,我试图显示相同的图像。

我已经用picasso下载了图像,并且在下载过程中一切正常。

当我将图像保存到SD卡中以显示到ImageView和WebView时,我遇到了一个问题:我的ImageView总是显示最后下载的图像,这没关系。但是,在我保存图像以显示到WebView之后,我的WebView始终显示下载的第一个图像而不是最后下载的图像。

拜托,我做错了什么?

    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());
        }
    }

1 个答案:

答案 0 :(得分:1)

这可能是由于图像被缓存在WebView中。

在显示第二张图片之前尝试调用webView.clearCache(true),它应该正常工作。

相关问题