Last active
December 18, 2015 14:59
-
-
Save pedronveloso/5801566 to your computer and use it in GitHub Desktop.
Class responsible for View Display storage on Internal Storage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.view.View; | |
import android.widget.ImageView; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.util.GregorianCalendar; | |
/** | |
* Class responsible for View Display storage on Internal Storage. | |
* <p/> | |
* User: Pedro Veloso <pedro.veloso@emerge.pt> | |
*/ | |
public final class ViewImageStorage { | |
// change this to change the default jpeg quality used to store the images | |
private final static int JPEG_QUALITY = 75; | |
// change this to define the color to fill the transparent area | |
private final static int BACKGROUND_COLOR = Color.BLACK; | |
/** | |
* Save the display of a View to the InternalStorage | |
* | |
* @param ctx Application Context | |
* @param view View to store on internal storage | |
* @return Name on internal storage, or empty if image not saved | |
*/ | |
public synchronized static String saveImageOnInternalStorage(Context ctx, View view) { | |
GregorianCalendar time = new GregorianCalendar(); | |
String saveName; | |
//append the unix timestamp to the filename. together with synchronized access it will result in unique naming | |
saveName = Long.valueOf(time.getTimeInMillis()).toString() + ".jpg"; | |
Bitmap bitmap; | |
if (view instanceof ImageView) { | |
// this is a ImageView, process it differently | |
//enable the drawing cache to draw the view as bitmap | |
view.setDrawingCacheEnabled(true); | |
bitmap = view.getDrawingCache(); | |
} else { | |
// is not an ImageView | |
bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
Drawable bgDrawable = view.getBackground(); | |
if (bgDrawable != null) { | |
bgDrawable.draw(canvas); | |
} else { | |
canvas.drawColor(BACKGROUND_COLOR); | |
} | |
view.draw(canvas); | |
} | |
// save the bitmap | |
try { | |
FileOutputStream fos = new FileOutputStream(ctx.getFilesDir().getAbsolutePath() + "/" + saveName); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, fos); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return ""; | |
} | |
return saveName; | |
} | |
/** | |
* Restore a saved image to a given ImageView. If the image does not exist then the | |
* ImageView will contain the drawable passed as a parameter. | |
* | |
* @param iv ImageView to display contents | |
* @param path Path of the stored image | |
* @param ctx Application Context | |
*/ | |
public static void restoreImageToImageView(Context ctx, ImageView iv, String path, Drawable noImage) { | |
if (path != null && path.length() > 0) { | |
File filePath = ctx.getFileStreamPath(path); | |
if (filePath.exists()) { | |
iv.setImageDrawable(Drawable.createFromPath(filePath.toString())); | |
} else { | |
if (noImage != null) { | |
// the given image does not exist in the internal storage, use noImage instead | |
iv.setImageDrawable(noImage); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment