|
안드로이드 예제 소스 모음 |
[1] |
|
등록일:2012-01-26 21:48:00 (0%) 작성자: 제목:Image Manager to handle SDCARD Image Saves and Gets |
|
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
public class ImageManager {
protected final static String STORAGE_PATH = Environment.getExternalStorageDirectory().toString();
private String imgUrl;
private String fn;
private static String APP_FOLDER_NAME = "/YOUR_APP_FOLDER_NAME/"
private static String basepath = STORAGE_PATH + APP_FOLDER_NAME;
public ImageManager(String imgUrl) {
this.setImgUrl(imgUrl);
fn = md5(imgUrl) + ".jpg";
}
public ImageManager(String imgUrl, boolean md5) {
this.setImgUrl(imgUrl);
if(md5 == false)
fn = imgUrl.ubstring(imgUrl.lastIndexOf("/") + 1);
else
fn = md5(imgUrl) + ".jpg";
}
public String getFilename() {
return basepath + this.fn;
}
protected Drawable getImageFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
is.close();
return d;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean fileExists() {
return new File(basepath+fn).exists();
}
public static String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest
.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
//e.printStackTrace();
return s;
}
}
public Bitmap getImage() {
Bitmap b = BitmapFactory.decodeFile(basepath+fn);
return (b.getWidth() == 0 && b.getHeight() == 0) ? null : b;
}
public Bitmap saveImage() {
Bitmap b = ((BitmapDrawable) getImageFromURL(imgUrl)).getBitmap();
if(b.getWidth() == 0 && b.getHeight() == 0) {
return null;
}
FileOutputStream out;
// Create dir
File dir = new File(basepath);
if(!dir.exists()) {
dir.mkdirs();
}
try {
File output = new File(dir, fn);
out = new FileOutputStream(output);
b.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {} catch (IOException e) {}
return b;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public String getImgUrl() {
return imgUrl;
}
}
// see http://androidsnippets.com/image-manager-to-handle-sdcard-image-saves-and-gets |
[본문링크] Image Manager to handle SDCARD Image Saves and Gets
|
[1]
|
|
|
|
|
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=31654 |
|
|
|
|
|
|
|
|
|
Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.
|
|
|