Skip to content

Instantly share code, notes, and snippets.

@iamdylanngo
Created March 9, 2018 01:55
Show Gist options
  • Save iamdylanngo/c8aad4b386ae76f3fa8b45389f3f8b55 to your computer and use it in GitHub Desktop.
Save iamdylanngo/c8aad4b386ae76f3fa8b45389f3f8b55 to your computer and use it in GitHub Desktop.
ImageManager.java
package jundat95.com.cachedata.ultis;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
/**
* Created by tinhngo on 3/9/18.
*/
public class ImageManager {
private static final String TAG = ImageManager.class.getSimpleName();
private String appName = "Test";
public ImageManager(String appName) {
this.appName = appName;
}
public boolean saveBitmap(String key, Bitmap value) {
boolean isTrue = false;
File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/Cache/" + appName + "/Image");
if (!mediaStorageDir.exists())
mediaStorageDir.mkdirs();
try {
OutputStream stream = new FileOutputStream(mediaStorageDir.getPath() + "/" + key + ".png");
value.compress(Bitmap.CompressFormat.PNG,100, stream);
stream.flush();
stream.close();
isTrue = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return isTrue;
}
public Bitmap getBitmap(String key) {
Bitmap bitmap = null;
File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/Cache/" + appName + "/Image");
try {
InputStream stream = new FileInputStream(mediaStorageDir.getPath() + "/" + key + ".png");
bitmap = BitmapFactory.decodeStream(stream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment