开发者

Android - Save image from internet to internal / external storage and display it in webview

I need to save image from inte开发者_开发问答rnet to internal / external storage and display it in webview locally. I have done retrieve image from internet. I need help to save it in internal or external storage and the path to display it in webview.


How to save image in sdcard from inputstream..

String dir = Environment.getExternalStorageDirectory().toString();
OutputStream fos = null;                
File file = new File(dir,"downloadImage.JPEG");
Bitmap bm =BitmapFactory.decodeStream(your input stream);
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();

Now your image is saved as download.JPEG in sdcard..

and you can get image using this path..

dir+"\downloadImage.JPEG"


for internal storage you can use SQlite or sharedpreference and for external storage check this tutorial:

http://www.vogella.de/articles/AndroidFileSystem/article.html


try {
           FileOutputStream out = new FileOutputStream(filename);
           bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
           e.printStackTrace();
    }

Fetch image from following function and save drawable in file

private Drawable LoadImageFromWeb(String url) {
  try {
   InputStream is = (InputStream) new URL(url).getContent();
   Drawable d = Drawable.createFromStream(is, "src name");
   return d;
  } catch (Exception e) {
   System.out.println("Exc=" + e);
   return null;
  }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜