Copy from Camra app:
public static final String CAMERA_IMAGE_BUCKET_NAME =
Environment.getExternalStorageDirectory().toString()
+ "/DCIM/Camera";
private ImageManager() {
}
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName =
Environment.getExternalStorageDirectory().toString() + "/DCIM";
File directory = new File(directoryName);
if (!directory.isDirectory()) {
if (!directory.mkdirs()) {
return false;
}
}
return directory.canWrite();
}
public static boolean hasStorage() {
return hasStorage(true);
}
public static boolean hasStorage(boolean requireWriteAccess) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess
&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
public static int getExifOrientation(String filepath) {
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
} catch (IOException ex) {
Log.e(TAG, "cannot read exif", ex);
}
if (exif != null) {
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
// We only recognize a subset of orientation tag values.
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
}
return degree;
}
//
// Stores a bitmap or a jpeg byte array to a file (using the specified
// directory and filename). Also add an entry to the media store for
// this picture. The title, dateTaken, location are attributes for the
// picture. The degree is a one element array which returns the orientation
// of the picture.
//
public static Uri addImage(ContentResolver cr, String title, long dateTaken,
Location location, String directory, String filename,
Bitmap source, byte[] jpegData, int[] degree) {
// We should store image data earlier than insert it to ContentProvider,
// otherwise we may not be able to generate thumbnail in time.
OutputStream outputStream = null;
String filePath = directory + "/" + filename;
try {
File dir = new File(directory);
if (!dir.exists()) dir.mkdirs();
File file = new File(directory, filename);
outputStream = new FileOutputStream(file);
if (source != null) {
source.compress(CompressFormat.JPEG, 100, outputStream);
degree[0] = 0;
} else {
outputStream.write(jpegData);
degree[0] = getExifOrientation(filePath);
}
} catch (FileNotFoundException ex) {
Log.w(TAG, ex);
return null;
} catch (IOException ex) {
Log.w(TAG, ex);
return null;
} finally {
Utils.closeSilently(outputStream);
}
// Read back the compressed file size.
long size = new File(directory, filename).length();
ContentValues values = new ContentValues(9);
values.put(Images.Media.TITLE, title);
// That filename is what will be handed to Gmail when a user shares a
// photo. Gmail gets the name of the picture attachment from the
// "DISPLAY_NAME" field.
values.put(Images.Media.DISPLAY_NAME, filename);
values.put(Images.Media.DATE_TAKEN, dateTaken);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, degree[0]);
values.put(Images.Media.DATA, filePath);
values.put(Images.Media.SIZE, size);
if (location != null) {
values.put(Images.Media.LATITUDE, location.getLatitude());
values.put(Images.Media.LONGITUDE, location.getLongitude());
}
return cr.insert(STORAGE_URI, values);
}
}
使用方法,如保存指定View到图片文件中:
if (false == mMainView.isDrawingCacheEnabled()) {
mMainView.setDrawingCacheEnabled(true);
}
Bitmap bitmap = mMainView.getDrawingCache();
// storeImageToFile(bitmap);
storeImage(bitmap, null);
mMainView.setDrawingCacheEnabled(false);
Toast.makeText(mContext, "Save photo: " + mLastContentUri, Toast.LENGTH_SHORT).show();