On Aug 19, 10:57 am, Pedro Teixeira <[email protected]> wrote:
> Does anyone know how to save the bitmap into a file in my device?
Hi Pedro,
Here's a function I've been using for quite a while:
private StringBuilder saveBitmap(String path, StringBuilder name,
Bitmap bitmap) {
StringBuilder result = new
StringBuilder(path).append('/').append(name);
if (DOLOG) Log.d(TAG, "saveBitmap: " + result);
File f = new File(path);
try {
if (!f.exists() && !f.mkdirs()) {
// Can't create path
result.setLength(0);
} else {
FileOutputStream fos = new
FileOutputStream(result.toString());
if (name.indexOf(".jpg") > -1) {
bitmap.compress(CompressFormat.JPEG, 90, fos);
} else {
bitmap.compress(CompressFormat.PNG, 100, fos);
}
fos.close();
}
} catch (IOException e) {
// Failed to create file - make sure there's no
incomplete remnant
left behind
Log.w(TAG, "Error opening " + result + " in saveBitmap: " +
e.toString());
File corruptFile = new File(path, name.toString());
if (corruptFile != null) {
corruptFile.delete();
}
result.setLength(0);
}
return result;
}
You need to pass it in the path & filename separately; it creates the
path if needed. In your case I assume you'll want to give it the path
of some temp dir, and you'll need to parse the name out of the URL you
start with, but neither of those is hard. It returns the full
filename, or an empty string if an error occurred.
It handles both JPEGs and PNGs, and it does some error checking; these
may or may not be appropriate for you. But it's something to start
with, anyway.
String
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en