StoneHeart
StoneHeart

Reputation: 16590

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

I need to save image from internet 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.

Upvotes: 0

Views: 6283

Answers (3)

Niranj Patel
Niranj Patel

Reputation: 33238

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"

Upvotes: 5

user756706
user756706

Reputation:

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

Upvotes: 1

Vineet Shukla
Vineet Shukla

Reputation: 24021

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

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

Upvotes: 1

Related Questions