1. 程式人生 > >資原始檔儲存到sd卡

資原始檔儲存到sd卡

/**
* 初始化分享的圖片
*/
private void initImagePath() {
public static String TEST_IMAGE;
try {//判斷SD卡中是否存在此資料夾
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
TEST_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/pic.png";
}
else {
TEST_IMAGE = getApplication().getFilesDir().getAbsolutePath() + "/pic.png";
}
File file = new File(TEST_IMAGE);
//判斷圖片是否存此資料夾中
if (!file.exists()) {
file.createNewFile();
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
FileOutputStream fos = new FileOutputStream(file);
pic.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
} catch(Throwable t) {
t.printStackTrace();
TEST_IMAGE = null;
}
}