Friday, September 18, 2015

Android Download and Save Image to Internal Memory

Hello,

Recently in one of my project. We have a requirement to download image from remote URL and save it to internal memory so every time image won't be downloaded but it will be displayed from internal memory.

First let's create a utility class and add static function to load and save image. Following is the UTIL class I used.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Utils {
    public static void saveFile(Context context, Bitmap b, String picName){ 
    FileOutputStream fos; 
    try
        fos = context.openFileOutput(picName, Context.MODE_PRIVATE); 
        b.compress(Bitmap.CompressFormat.PNG, 100, fos); 
        fos.close(); 
    }  
    catch (FileNotFoundException e) { 
        Log.d("file", "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d("file", "io exception"); 
        e.printStackTrace(); 
    } 

}
    
    public static Bitmap loadBitmap(Context context, String picName){ 
    Bitmap b = null
    FileInputStream fis; 
    try
        fis = context.openFileInput(picName); 
        b = BitmapFactory.decodeStream(fis); 
        fis.close(); 

    }  
    catch (FileNotFoundException e) { 
        Log.d("file", "file not found"); 
        e.printStackTrace(); 
    }  
    catch (IOException e) { 
        Log.d("file", "io exception"); 
        e.printStackTrace(); 
    }  
    return b; 
}

}

As you can see above it has two functions one is to save image and one is to load saved image. Now first we will check if if image is already saved or not.

ImageView imgView = new Image(getActivity());
Bitmap b = Utils.loadBitmap(getActivity(),"myImage.png");
if(b == null){
       new DownloadImageTask(imgView,"myImage.png").execute("http://remoteimagepath");
}else{
      imgView.setImageBitmap(b);
}

Now we have to add DownloadImageTask class as follow.

private class DownloadImageTask extends AsyncTask {
ImageView imageView = null;
        String name = "";
public DownloadImageTask(ImageView img, String name) {
this.imageView = img;
                this.name = name;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
Utils.saveFile(getActivity(),result, this.name);
this.imageView.setImageBitmap(result);
}
}

As you can see above in this class we are passing reference of imageView and when image is downloaded we setup bitmap to imageview and save image to local memory with name which we passed in parameter. 

Hope this helps you.

No comments:

Post a Comment