这是我的代码。您将看到我希望将Drawable d保存到File f位置的位置:
ImageView imgView = (ImageView)header.findViewById(R.id.imvCover);
File cacheDir = this.getCacheDir();
File f = new File(cacheDir, itemDict.get("Barcode") + ".jpg");
if (f.exists())
{
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
imgView.setImageBitmap(bmp);
}
else
{
String url = (String)itemDict.get("imageURL");
InputStream is = null;
try {
is = (InputStream) new URL(url).getContent();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(is, "src");
if (d != null)
{
imgView.setImageDrawable(d);
// TODO: save the drawable into the cache directory
}
else
{
imgView.setImageResource(R.drawable.cam);
}
} 如何将可绘制文件另存为.jpg?
发布于 2012-11-13 08:21:53
使用Bitmap.compress(...),如下所示:
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream("/someLocation/someFileName.jpg"));
} catch (Exception e) {
//TODO: Handle exception
}有关如何获取缓存目录路径的示例,请参阅此SO答案:
android-download-images-from-server-and-save-them-on-device-cache
这是关于从Drawable到Bitmap的转换
https://stackoverflow.com/questions/13353735
复制相似问题