我的工作的功能从Web服务器下载图像,在屏幕上显示出来,如果用户希望保留图像,将其保存在SD卡上的一个特定的文件夹。有一种简单的方法,采取一个位图,只是将其保存到SD卡上我选择的文件夹中?
我的问题是,我可以下载图片,作为位图显示在屏幕上。我已经能够找到的图像保存到特定文件夹的唯一方法是使用FileOutputStream中,但是这需要一个字节数组。我不知道如何转换(如果这是连正确的方式),从位图到字节数组,这样我就可以用一个FileOutputStream来写入数据。
另一种选择我是使用MediaStore:
MediaStore.Images.Media.insertImage(getContentResolver(),BM,
酒吧codeNumber +的.jpg卡图片,酒吧codeNumber +的.jpg卡图像);
工作正常,保存到SD卡上,但不允许您自定义的文件夹。
解决方案 的FileOutputStream了= NULL;
尝试 {
OUT =新的FileOutputStream(文件名);
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,出); // BMP是你的Bitmap实例
// PNG是无损格式时,COM pression因子(100),则忽略
}赶上(例外五){
e.printStackTrace();
} 最后 {
尝试 {
如果(满分!= NULL){
out.close();
}
}赶上(IOException异常E){
e.printStackTrace();
}
}
I am working on a function to download an image from a web server, display it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain folder. Is there an easy way to take a bitmap and just save it to the SD card in a folder of my choice?
My issue is that I can download the image, display it on screen as a Bitmap. The only way I have been able to find to save an image to a particular folder is to use FileOutputStream, but that requires a byte array. I am not sure how to convert (if this is even the right way) from Bitmap to byte array, so I can use a FileOutputStream to write the data.
The other option I have is to use MediaStore :
MediaStore.Images.Media.insertImage(getContentResolver(), bm,
barcodeNumber + ".jpg Card Image", barcodeNumber + ".jpg Card Image");
Which works fine to save to SD card, but does not allow you to customize the folder.
解决方案FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}