【问题标题】:Save image to sdcard in Android在Android中将图像保存到SD卡
【发布时间】:2015-09-01 01:12:56
【问题描述】:

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

我已经用毕加索下载了图片,下载过程中一切正常。

当我将图像保存到 sdcard 以显示到 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());
        }
    }

【问题讨论】:

    标签: android image webview save


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。你是对的。这解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多