/**
* 从drawable把图片放入data
*
* @return File
* @param context
* @param resId
*/
try {
// path==/storage/emulated/0/Pictures
File LogoFileRoot = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//Log.i("tag", "path==" + LogoFileRoot.getPath());
// 计算图片存放全路径
// String LogoFilePath = LogoFileRoot+"/"+"a.jpg";
File dir = new File(LogoFileRoot.getPath());
if (!dir.exists()) {
dir.mkdirs();
}
File file = null;
// 如果文件夹不存在,创建一个(只能在应用包下面的目录,其他目录需要申请权限 OWL)
if (dir.isDirectory()) {
file = new File(dir.getPath() + dir.separator + "a.png");
}
// 获得封装 文件的InputStream对象
is = context.getResources().openRawResource(resId);
fos = new FileOutputStream(file);
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制Logo图片文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* file.getPath()从data目录拿到bitmap对象
* @param url
* @return bitmap
*/
public static Bitmap getBitmap(String url){
try {
//把图片文件打开为文件流,然后解码为bitmap
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}