资源文件保存到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;
}
}

猜你喜欢

转载自blog.csdn.net/u014035162/article/details/34416465
今日推荐