Saving image, webview, android

后端 未结 2 1970
情深已故
情深已故 2020-12-10 20:40

I am using webview in android to display images (mainly using google ajax API), Now if I want to save an image into local storage, How do I do ? I have image url, which can

2条回答
  •  猫巷女王i
    2020-12-10 21:06

    I know this is a quite old but this is valid too:

    Bitmap image = BitmapFactory.decodeStream((InputStream) new URL("Http Where your Image is").getContent());
    

    With the Bitmap filled up, just do this to save to storage (Thanks to https://stackoverflow.com/a/673014/1524183)

    FileOutputStream out;
    try {
           out = new FileOutputStream(filename);
           image.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
           try{
               out.close();
           } catch(Throwable ignore) {}
    }
    

    IHMO much more cleaner and simpler than the accepted answer.

提交回复
热议问题