保存图片至SD卡

来源:互联网 发布:怎么永久删除软件 编辑:程序博客网 时间:2024/05/29 07:18
package alice.bw.com.app2;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class SDcardUtil {


// 保存图片至SD卡
public static void saveBitmapToSD(String key, Bitmap bitmap, Context mContext) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = null;
String path = mContext.getExternalCacheDir().getAbsolutePath() + File.separator
// +key;
+ key.substring(key.lastIndexOf("/") + 1);
Log.d("sxl", "saveBitmapToSD: "+path);
file = new File(path);


FileOutputStream outputStream = null;


try {
file.createNewFile();
outputStream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}


}
}


// 从SD卡中将图片取出来
public static Bitmap getBitmapFromSD(String key, Context mContext) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String path = mContext.getExternalCacheDir().getAbsolutePath() + File.separator
// +key;
+ key.substring(key.lastIndexOf("/") + 1);
Bitmap bitmap = BitmapFactory.decodeFile(path);
return bitmap;
}


return null;
}
}
原创粉丝点击